Simulate _GET php in string

Question:

I have a simulated string:

<li><a title="string" href="http://geting.com/?v=123?t=abc">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="http://geting.com/?v=123?t=abc">Opção 1</a></li>';
echo preg_match('regex',$string);

Desired result: 123

Answer:

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 = "http://geting.com/?v=123&t=abc";
$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 =)

*Edition

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

$li = '<li><a title="string" href="http://geting.com/?v=123&t=abc">Opção 1</a></li>';
preg_match_all("/\"(.*?)\"/",$li,$matches);
print_r($matches);
Scroll to Top