0

I need to parse through a list of url strings and extract certain parameter values.

These strings are from a database NOT using POST or GET.

Here is an example url string and the goal is to extract the user_id part of the string.

https://www.example.com/?action=get_my_messages&user_id=9825&session_token=1463812ecbfa4

NOTE: the parameter order of the string might be different with different strings and in some cases the user_id might not even exist.

FOUND SOLUTION

$query = parse_url($request_string, PHP_URL_QUERY);
parse_str($query, $params);
$user_id= $params['user_id'];

OBTAINED THE SOLUTION FROM THIS POST BELOW

How do I extract query parameters from a URL string in PHP?

9
  • Everything here will be in the $_GET array. No need to parse or regex. For example $_GET['user_id'] will have your value. Commented Oct 31, 2016 at 15:43
  • 1
    did you try something ? Commented Oct 31, 2016 at 15:43
  • parse_url + parse_str Commented Oct 31, 2016 at 15:44
  • That's overkill @iainn Commented Oct 31, 2016 at 15:44
  • @JayBlanchard It sounds like these URLs are coming from a file/etc, rather than just being the URL of the script. But maybe not. Commented Oct 31, 2016 at 15:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.