Reputation: 656
How can I match both (http://[^"]+)
's?:
<a href="http://yoursite.com/goto/http://aredirectURL.com/extraqueries"></a>
(I know it's an illegal URL, but same idea)
I want the regex to give me these two matches:
1 http://yoursite.com/goto/http://aredirectURL.com/extraqueries
2 http://aredirectURL.com/extraqueries
Without running multiple preg_match_all's
Really stumped, thanks for any light you can shed.
Upvotes: 0
Views: 2187
Reputation: 43673
$str = '<a href="http://yoursite.com/goto/http://aredirectURL.com/extraqueries"></a>';
preg_match("/\"(http:\/\/.*?)(http:\/\/.*?)\"/i", $str, $match);
echo "{$match[0]}{$match[1]}\n";
echo "{$match[1]}\n";
Upvotes: 0
Reputation: 6665
This regular expression will get you the output you want: ((?:http://[^"]+)(http://[^"]+))
. Note the usage of the non-capturing group (?:regex)
. To read more about non-capturing groups, see Regular Expression Advanced Syntax Reference.
<?php
preg_match_all(
'((?:http://[^"]+)(http://[^"]+))',
'<a href="http://yoursite.com/goto/http://aredirectURL.com/extraqueries"></a>',
$out);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
The above code outputs the following:
Array
(
[0] => Array
(
[0] => http://yoursite.com/goto/http://aredirectURL.com/extraqueries
)
[1] => Array
(
[0] => http://aredirectURL.com/extraqueries
)
)
Upvotes: 1
Reputation: 849
you can split the string with this function:
each part can contain e.g. one of the urls in the array given in the result.
if there is more content maybe call the preg_split using a callback operation while your full text is "worked" on.
Upvotes: 0