Simulate _GET php in string


I have a simulated string:

<li><a title="string" href="">Opção 1</a></li>

I would like something that takes the ?v= parameter, just it, just like when we get the $_GET["v"] .

How could I do this? I believe preg_match could help me, but I don't understand about?

$string = '<li><a title="string" href="">Opção 1</a></li>';
echo preg_match('regex',$string);

Desired result: 123


I believe your link is incorrect the normal format is ?v=123&t=abc , ie the second ? must be an & .

I think you can use two php functions for this, the parse_url and parse_str

With the first one you parse the url and get the query .

The second transforms the query into an array.

$url = "";
$parse = parse_url( $url );
parse_str( $parse['query'],$query );
echo $query['v'];

Note: The code above has not been tested, but that's the idea =)


If you need to extract the url from the text, you can use the following code

$li = '<li><a title="string" href="">Opção 1</a></li>';
Scroll to Top