Mortaza Faryabi Mortaza Faryabi - 1 year ago 67
PHP Question

Extract string in brackets when there are other brackets embedded in quotes

I want to extract this bracketed part from a string:

[list items='["one","two"]' ok="no" b="c"]

I am using the following

preg_match('~\[([a-zA-Z0-9_]+)[ ]+([a-zA-Z0-9]+=[^\[]+)\]~s', $string,$match)

But I have trouble with the brackets that appear within quotes.

I have two files


[list items=""one","[x]tw"'o"" ok="no" b="c""/]
[button text="t'"extB1" name="ok"'" /]
Asdfz " s wr aw3r '
[button text="t"'extB2" name="no"'" /]


for (;;) {
if (!preg_match('~\[([a-zA-Z0-9_]+)[ ]+([a-zA-Z0-9]+=[^\[]+)\]~s', $string,$match)) {
$string=str_replace($match[0], '', $string);
echo "<pre><br>";
echo "<br></pre>";

and this is output:

[0] = [button text="textB1" name="ok"]
[1] = button
[2] = text="textB1" name="ok"
[0] = [button text="textB2" name="no"]
[1] = button
[2] = text="textB2" name="no"

As you can see the output does not include

[list items='["one","two"]' ok="no" b="c"]

I know the problem is caused by the embedded square brackets, but I don't know how I can correct the code to ignore them.

Answer Source

You could use this variation of your preg_match call:

if (!preg_match('~\[(\w+)\s+(\w+=(?:\'[^\']*\'|[^\[])+?)\]~s', $string, $match))

With \'[^\']*\' it detects the presence of a quote and will grab all characters until the next quote, without blocking on an opening bracket. Only if that cannot be matched, will it go for the part you had: [^\[])+. I added a ? to that, to make it non-greedy, which makes sure it will not grab a closing ].

Note also that [a-zA-Z_] can be shortened to \w, and [ ] can be written as \s which will also allow other white-space, which I believe is OK.

See it run on

Alternative: match complete lines only

If the quotes can appear anywhere without guarantee that closing brackets appear within quotes, then the above will not work.

Instead we could require that the match must span a complete line in the text:

if (!preg_match('~^\s*\[(\w+)\s+(\w+=.*?)\]\s*$~sm', $string, $match))

See it run on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download