/    Sign up×
Community /Pin to ProfileBookmark

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 [B]“=”[/B] 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 [b]after[/b] the [B]“=”[/B] 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

[code=php]$urlArr2 = preg_match(“/[?=w=]+/”,$urlEl,$match);[/code]

  • How would I enhance this function to store a key => value pair for future comparison / action
  • [code=php]
    <?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>nt”;
    }
    }
    return $pageURL;
    }
    splitURL($_SERVER[‘QUERY_STRING’]);
    [/code]

    Many thanks

    to post a comment
    PHP

    3 Comments(s)

    Copy linkTweet thisAlerts:
    @eval_BadCode_Apr 25.2011 — I am taking the URL's query string and trying to store the contents into an array for future use. [/QUOTE]

    [code=php]
    //already exists .... in an array
    $_SERVER['argv'];
    [/code]

    http://us.php.net/manual/en/reserved.variables.argv.php

    Try it for yourself... (nice and formatted output for you):

    http://vircaelum.net/server.php?example=you&#37;20mean%20you%20wanted%20this

    you'll notice the %20 and not a " "... you should be able to split on "=" and "&" without any trouble.


    store the contents into an array for future use.[/QUOTE]
    [code=php]
    // another 1 liner (fixed length + serialization)
    <?php

    file_put_contents("argv_log.log", str_pad($_SERVER[REQUEST_TIME], 15) . serialize($_SERVER) . "n", FILE_APPEND);

    ?>
    [/code]


    Best of luck.
    Copy linkTweet thisAlerts:
    @gingerjauthorApr 25.2011 — 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:
    [code=php]$urlArr2 = preg_split("/^[^=]*/",$urlEl,null);[/code]

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

    ** I have found this intellectuality stimulating.
    Copy linkTweet thisAlerts:
    @NogDogApr 25.2011 — Maybe I'm confused, but isn't what you're trying to do already in the $_GET array? If not, then the next thing would be to use the [url=http://www.php.net/manual/en/function.parse-str.php]parse_str[/url]() function.
    ×

    Success!

    Help @gingerj spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...