如何拉取elseif - preg_match_all
问题描述:
I need advise how to pull content from this string.
$string = "{elseif "xxx"=="xxx"} text {elseif "xx2"!="xx2"}
text text
text
{elseif ....} text";
//or 'xxx'=='xxx'
$regex = "??";
preg_match_all($regex, $string, $out, PREG_SET_ORDER);
var_dump($out);
And my idea of var_dump output is:
array
0 =>
array
0 => string 'xxx' (length=3)
1 => string '==' (length=2)
2 => string 'xxx' (length=3)
3 => string 'text' (length=4)
1 =>
array
1 => string 'xx2' (length=)
2 => string '!=' (length=)
3 => string 'xx2' (length=)
4 => string 'text text
text' (length=)
2 =>
array
...
The output need not necessarily be as follows, but the same content.
my attempt:
$regex = "~{elseif ([\"\'](.*)[\"\'])(!=|==|===|<=|<|>=|>)([\"\'](.*)[\"\'])}(.*)~sU";
But I have bad or no output content.
答
Do you mean something like this? If you want to test it.
$regex = "/\{\s*elseif\s*(\"[^"]+\")\s*([^"]+)\s*(\"[^"]+\")\s*\}\s*([^{]*)\s*/gi";