Small error with my regular expression

Hello,

I am taking the URL’s query string and trying to store the contents into an array for future use.

I thought I cracked it this morning, but my regular expression is wrong.

I would ideally like:

  • To store each query string element into a key/value array
  • But but my limited skillset has restricted me to storing just the key into an array

My regular expression finds matches but it was not what I expected:

  • it prints a series of “=” onto my page.

I find regular expressions a confusing syntax - could anyone let me know what I have done wrong.

What would be nice:

  • Tell me what is wrong with my reg ex today why does it not print the contents of the url after the “=” and not just the “=”
    I have also failed in an alternative pattern, it prints everything!!! I assumed the following tells the server to… find a word before a “=” give me what “=” is suffixed with
$urlArr2 = preg_match("/[?=\\w=]+/",$urlEl,$match);
  • How would I enhance this function to store a key => value pair for future comparison / action

<?php
function splitURL($url)
{
    $pageURL = urldecode($url);
    $pageURL = parse_url($pageURL,PHP_URL_PATH);
    $urlArr = preg_split("/[\\&]+/",$pageURL,null);
        foreach($urlArr as $urlEl)
        {
            $urlArr2 = preg_match("/[?==]+/",$urlEl,$match);
                foreach($match as $urlEl1)
                {
                    echo "<h5>$urlEl1</h5>\
\	";                     
                }
        }
    return $pageURL;
}
splitURL($_SERVER['QUERY_STRING']);

Many thanks

  1. Wait with the decode.
  2. Instead of regular expressions, use ‘explode’.

  $first_split=explode("?", $pageUrl);
  $query=$first_split[1]; // Query - what follows the '?'.
  $vars=explode('&',$query);
  foreach ($vars as $var){
    $tokens=explode('=', $var);
    $var_name=urldecode($tokens[0]); // Left of the equal sign
    $var_value=urldecode($tokens[1])); // Right of the equal sign
  }


The part of the URL containing the variables is called ‘Query’, you can get it by passing PHP_URL_QUERY as the 2nd param to parse_url.

PHP already does that auto-magically for you, see the sections in the PHP manual which cover external variables and in particular [url=http://php.net/$_GET]$_GET.

Hi,

Thank you for your assistance, call me stupid but I am like a dog with a bone right now.

I have managed to do some reading & can extract the contents of the string either side of the “=”.

Using this:

$urlArr2 = preg_split("/^[^=]*/",$urlEl,null);

Just wondering if someone could assist with removing the “=”, or perhaps I should call it quits and use some sort of trim function.

** I have found this intellectuality stimulating.

You could try explode by = or use str_replace =
Explode I think would be a better solution.

Just in case you wanted to do this natively. :wink:


<?php
$url = 'http://www.example.org/?a=1&b=2&c=3&d=4';

$params = array();

parse_str(parse_url($url, PHP_URL_QUERY), $params);

var_dump(
  $params
);

/*
  array(4) {
    ["a"]=>
    string(1) "1"
    ["b"]=>
    string(1) "2"
    ["c"]=>
    string(1) "3"
    ["d"]=>
    string(1) "4"
  }
*/