Jump to content

[SOLVED] problems with recursive on advanced (a(b)c)


msaspence

Recommended Posts

i need to pull some segments out of a file (loaded into a variable)

 

the segments i need to pull out is basically anything in brackets

 

what makes its esp hard is the brackets are not your standard () [] or {}, the closing bracket character is in fact two characters

 

for example

from the string:

£a£b£c!£d!£e!£ f £g!£

where £ equates to (

and !£ equates to )

 

i want to extract

£a£b£c!£d!£e!£

and

£g!£

as matches

 

so far i have got to:

 

$string = " §b§c;§d;§";
if(preg_match_all("/(?<!§([^()]+|(?R))*(?<=§/U",$string,$matches))	{
     echo "<pre>"; print_r($matches[0]); echo "</pre>";
}

 

which returns

 

Array
(
    [0] => £a£b£c!£
    [1] => £g!£
)

 

the first match is missing the d and e parts

 

Any clues on a solution would be really appreciated

I'm not sure why you're using section marks or semicolons, but here is an example:

 

<pre>
<?php
$string = '£a£b£c!£d!£e!£ f £g!£';
preg_match_all('/£ (?: (?>[^!£]+) | (?R) )* !£/x', $string, $matches);
print_r($matches);
?>
</pre>

 

yields:

 

Array

(

    [0] => Array

        (

            [0] => £a£b£c!£d!£e!£

            [1] => £g!£

        )

 

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.