Simulate _GET php in string

Asked

Viewed 39 times

3

I have a simulate string to:

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

I’d like something to take the parameter ?v=, just him, just like when we took the method $_GET["v"].

How could I do that? 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

  • https://ideone.com/wHNt0v

1 answer

2


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

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

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

The second transforms the query in 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 this is the idea =)

*Editing

In case 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);
  • 1

    Correct! Thank you very much for your time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.