輸入(I加倍效果字符串):
$string='test{{this should not be selected and the curly brackets too}} but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets. test{{this should not be selected and the curly brackets too}} but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets.';
方法#1 preg_split()
:
var_export(preg_split('/{{[^}]*}}/',$string,null,PREG_SPLIT_NO_EMPTY));
// Added the fourth param in case the input started/ended with a double curly substring.
方法#2 preg_match_all()
:
var_export(preg_match_all('/(?<=}{2}|^)(?!{{2}).*?(?={{2}|$)/s',$string,$out)?$out[0]:[]);
輸出(無論哪種方式):
array (
0 => 'test',
1 => ' but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets. test',
2 => ' but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets.',
)
preg_split()
待雙捲曲纏繞子爲「分隔符」和分裂他們完整的字符串。
的preg_match_all()
方法圖案... Pattern Demo此使用正回顧後和正超前兩者追捕雙花括號或啓動弦/結束的。它在中間使用負向預覽,以避免在新行開始時匹配不需要的雙捲曲字符串。最後,模式末尾的s
修飾符將允許.
匹配換行符。
用'preg_split'拆分文本 –
一個很奇怪的任務。 '〜{{。*?}}(* SKIP)(* F)|(?:(?!{{。*?}})。)+〜s'應該可以工作。 –