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

Link to comment
Share on other sites

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!£

        )

 

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.