Simulate _GET php in string

3

I have a string to simulate:

<li><a title="string" href="http://geting.com/?v=123?t=abc">Opção 1</a></li>

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

How could you do this? I believe preg_match could help me, but I do not 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 outcome: 123

    
asked by anonymous 26.11.2018 / 20:54

1 answer

2

I think your link is incorrect and the normal format is ?v=123&t=abc , ie the second ? should be & .

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

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

The second transforms 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 above code has not been tested, but that's the idea =)

* Edition

If you need to extract the url of 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);
    
26.11.2018 / 21:11