如何排除preg_split
正斜杠后跟单引号括起来的字符串?
我已经对这个问题感到很震惊。
下面是代码,后跟输出。
$str = "storeid='1,2',categoryname = 'the ultimate \'operating room'";
$match = preg_split("/('[^'\\\\]*')/U", $str, null, PREG_SPLIT_DELIM_CAPTURE);
print_r($match);
输出
Array ( [0] => storeid= [1] => '1,2' [2] => ,categoryname = 'the ultimate \ [3] => 'operating room' [4] => )
但这是我的预期输出,我想排除\'。
希望有人理解并帮助我。
Array ( [0] => storeid= [1] => '1,2' [2] => ,categoryname =[3] => 'the ultimate \'operating room' )
答案 0 :(得分:0)
虽然捕获用单引号括起的所有内容,但您也应该注意escpaed单引号。遵循这个正则表达式:
'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'
'
单引号(开头)[^'\\\\]*
除'
和\
(?:\\\\.[^'\\\\]*)*
匹配转义字符(如果存在),然后尽可能使用相同的先前模式'
单引号(结束)PHP:
$match = preg_split("/('[^'\\\\]*(?:\\\\.[^'\\\\]*)*')/",
$str,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
输出:
Array
(
[0] => storeid=
[1] => '1,\'2'
[2] => ,categoryname =
[3] => 'the ultimate \'operating room'
)