我希望使用preg_split在双正斜杠(//)和问号(?)上拆分字符串。我可以使用这样的一个正斜杠:
preg_split('[\/]', $string);但
preg_split('[\//]', $string);
or
preg_split('[\/\/]', $string);不要工作。
如何处理表达式中的双正斜杠?
发布于 2014-05-06 07:19:35
如您所见,字符串在两个正斜杠(//)和一个问号(?)处被拆分。
$str= 'Hi//Hello?New World/';
$splits = preg_split( '@(\/\/|\?)@', $str );
print_r($splits);OUTPUT :
Array
(
[0] => Hi
[1] => Hello
[2] => New World/
)

发布于 2014-05-06 07:15:27
/ and ?是为正则表达式保留的,当您按字面意思使用它们时,您需要转义它们。
$str = 'test1 // test2 // test3 ';
$array = preg_split("/\/\//", $str);
print_r($array);为了什么?你可以用
$str = 'test1 ? test2 ? test3 ';
$array = preg_split("/\?/", $str);
print_r($array);发布于 2014-05-06 07:17:43
您可以为正则表达式使用不同的分隔符。
<?php
$str = 'hello // world // goodbye ? foo ? bar';
$array = preg_split("!//!", $str);
print_r($array);
?>对于问号,您可以使用字符类来封装它。您可以使用|进行一次或另一次的分离。
$array = preg_split("!//|[?]!", $str);https://stackoverflow.com/questions/23488185
复制相似问题