这是我的字符串:
$myString = "first second third,el forth, fiveeee, six";
我想要捕捉的是:
first
second
third
el forth
fiveeee
six
这是我尝试在 preg_split 中使用正则表达式的方法:
$myPattern = "[\s,]";
问题是这分别捕获了“el”和“forth”..
我怎样才能欺骗它来捕获 el for 呢?
编辑:
我没说清楚.. 我想将 el 捕获为单个数组元素..因为 EL 太短..我认为它是一个单词。喜欢:
EL CLASSICO,SOMETHING DIFFERENT,SOMETHINGELSEHERE SOMEMORETEXT
应该是:
* `EL CLASSICO`
* `SOMETHING DIFFERENT`
* `SOMETHINGELSEHERE`
* `SOMEMORETEXT`
它们应该用空格或逗号分隔,但如果有 EL 或 LE 之类的东西,应该被忽略。
<?php
$myString = "first second third,el forth,del fiveeee,six,six seven,four six";
$myPattern = "/\s*,\s*|(?<=[^\s,]{4})[\s,]+/";
print_r(preg_split($myPattern, $myString));
?>
产生
[0] => first
[1] => second
[2] => third
[3] => el forth
[4] => del fiveeee
[5] => six
[6] => six seven
[7] => four
[8] => six
(?<=[^\s,]{4})
是一个积极的后向断言。仅当前面有四个非分隔符字符时它才会成功(但它不匹配字符本身,它只检查它们是否存在)。如果前一个单词太短,这允许它不会拆分。
但如果分隔符包含逗号,它总是会被分割——这就是 \s*,\s*|
的用途。
问题编辑后没有好的解决方案,igrone
只是
str_replace( ',' , ' ' , $myString)
最终str_replace( ' ', ' ' , $myString)
以避免双空格或:
preg_replace( '@, ?' , ' ' , $myString)
匹配零个或多个“小单词”,然后在分割分隔字符之前匹配整个单词。
(?:[^ ,]{1,3} )* #match one to three non-space/non-comma characters followed by a space zero or more times
[^ ,]+ #match on or more non-space/non-comma characters
\K #forget the previously matched characters
[ ,]+ #match one or more spaces or commas
$string = "first second third,el forth,del fiveeee,six,six seven,four six";
var_export(
preg_split('/(?:[^ ,]{1,3} )*[^ ,]+\K[ ,]+/', $string)
);
输出:
array (
0 => 'first',
1 => 'second',
2 => 'third',
3 => 'el forth',
4 => 'del fiveeee',
5 => 'six',
6 => 'six seven',
7 => 'four',
8 => 'six',
)