0

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
tweak2
  • 646
  • 5
  • 15
  • Arbitrarily nested, or up to two? (The former is impossible with a single PHP regular expression.) – Ry- Apr 19 '12 at 23:05
  • 2
    @minitech [recursive regexes man!](http://stackoverflow.com/a/3180176/31671) :P – alex Apr 19 '12 at 23:15

3 Answers3

1

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
        )

)
creemama
  • 6,559
  • 3
  • 21
  • 26
  • Perfect, thanks! Here is what I ended up doing: `/((?:http[^\"\'\s>]+)?(http[^\"\'\s>]+))/gism` – tweak2 Apr 19 '12 at 23:50
0

you can split the string with this function:

http://de.php.net/preg_split

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.

Hajo
  • 849
  • 6
  • 21
0
$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";
Ωmega
  • 42,614
  • 34
  • 134
  • 203