tweak2
tweak2

Reputation: 656

Is there a way to match recursively/nested with regex? (PHP, preg_match_all)

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

Answers (3)

Ωmega
Ωmega

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

creemama
creemama

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

Hajo
Hajo

Reputation: 849

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.

Upvotes: 0

Related Questions