简体   繁体   中英

Regular Expressions in PHP

Sorry for unclear description, my English is not good.

My problem is that I want to decode a string, and this string has nested content delimited by {}.
For example:

The string:

{any string0{any string 00{any string 000....}}}{any string1}any string.

The result I want to get:

 array[0] = {any string0{any string 00{any string 000....}}}
 array[1] = {any string1}

I hope it's clear enough.

Making the best use of the (oddly put, and hopefully soon-to-be-edited) question, the following takes your example string and provides your example array.

$subject = '{blah\blah{\blah\blah...{\bl....}}}{blah...}blah... ';
$pattern = '/\{(?>[^{}]++|(?R))*\}/';

preg_match_all($pattern, $subject, $matches);
print_r($matches[0]);

Which produces:

Array
(
    [0] => {blah\blah{\blah\blah...{\bl....}}}
    [1] => {blah...}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM