Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

pulling specific variable from querystring URL

Last post 02-12-2008, 6:05 PM by sanmi. 5 replies.
Sort Posts: Previous Next
  •  02-12-2008, 5:16 PM 39535

    pulling specific variable from querystring URL

    Hi,

    I am using PHP5 on a XAMP installation.

    I am trying to extract a specific variable from a querystring (which is stored in my MySQL DB)

    The format of the string is always consists of the same parameters (p, x & y), however sometimes they could be in a different order.

    A sample querysting is below and I am trying to access the value of the 'p' parameter (which is always numerical) ...

    http://subdir.domain.com/click?p=54321&x=12345&y=123456

    so in this case im looking to print out:  '54321'

    I assume one way of doing this would be to get the numbers after "p=" and before the following "&", or will there be limitations on this?

     It would be very helpful if you could post the php function or even the string to do this and output the 'p' variable

    When I have identified the value I am writing this into an xml file.

     thanks

    Sam

     

  •  02-12-2008, 5:20 PM 39536 in reply to 39535

    Re: pulling specific variable from querystring URL

    Not a regex question, but in PHP you could use:

    if (isset($_GET["p"])) {
      $p = $_GET["p"];
    }


  •  02-12-2008, 5:32 PM 39537 in reply to 39536

    Re: pulling specific variable from querystring URL

    The querystring is stored as a string in a DB, im not callling the URL so I guess am not able to request is via GET?

    So I guess I should be doing a reg expression??

    thanks

     

     

     

  •  02-12-2008, 5:40 PM 39538 in reply to 39535

    Re: pulling specific variable from querystring URL

    \bp=(\d+)

    Get the value from group 1.

    Sorry you'll have to write your own code. 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  02-12-2008, 6:00 PM 39540 in reply to 39538

    Re: pulling specific variable from querystring URL

    not having much luck with a preg_match function.

     if Im just extracting the variable would it be best to use a ereg() in PHP?

    thanks

     sam

  •  02-12-2008, 6:05 PM 39541 in reply to 39540

    Re: pulling specific variable from querystring URL

    ok.. I think I have it now ;-)

    $URL = "http://subdir.domain.com/click?p=54321&x=12345&y=123456";

    preg_match('/\bp=(\d+)/', $URL, $matches);
    echo "p=: {$matches[1]}\n";

    ###

     p=: 54321

    ###

    Is this the most recomended method?

    thanks

    Sam

View as RSS news feed in XML