Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End parse url-like query variables

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #145241
    makeshift67
    Participant

    I worked a little while back on a function to create some list-item links by passing something like :

    _li('href=home.php&text=Home&target=_blank');

    and return them as variables:

    $href = 'home.php',
    $text = 'Home',
    $target = 'blank'

    I had gotten the idea from some wordpress function I had been using that allowed me to pass variables in a similar way; unsuccessful in finding out how they did it, I started researching and came up with this function. It has been revised a couple of times and more recently I had added the extract() function which takes an array and creates variables using the key value as the name of the variable ($name) and setting the value within the same array variable.

    This function can do much more than parsing variables for links and I figured I would share it with anyone who wants can benefit from it. I would also love input on anyway to make this function better/’more-correct’.

    function parse($args){
       $args = explode('&', $args);
            $c = 0;
    
       foreach($args as $v){
          $i = explode('=', $v);
          $newArgs[$c][$i[0]] = $i[1];
          ++$c;
       }
       unset($args);
            
        foreach($newArgs as $v){
          foreach($v as $kk => $vv){
             $args[$kk] = $vv;
          }
       }
       return $args;
    }
            
     // usage
    $parse = parse('text=Home&href=/home.php&target=_blank');
    extract($parse);
    
     echo '<', 'a href="', $href, '" target="', $target, '">', $text,'</a', '>';

    you can also return $newArgs for an array:

    Array([text] => 'Home', [href] => '/home.php', [target] => '_blank')

    edit the code for your own, make it awesome.

    php.net extract() documentation

    Also, here’s a great site for practicing php online.

    #145243
    makeshift67
    Participant
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.