任何人都可以向我解释如何使用 preg_split() 函数吗?
我不理解这样的模式参数 "/[\s,]+/"
。
例如:
我有这个主题:is is.
我希望结果是:
array (
0 => 'is',
1 => 'is',
)
所以它会忽略空格和句号,我该怎么做?
最佳答案
preg
表示 Pcre REGexp",这有点多余,因为“PCRE”表示“Perl 兼容正则表达式”。
正则表达式是初学者的噩梦。我仍然不完全理解他们,我已经和他们一起工作多年了。
基本上你那里的例子,分割是:
"/[\s,]+/"
/ = start or end of pattern string
[ ... ] = grouping of characters
+ = one or more of the preceeding character or group
\s = Any whitespace character (space, tab).
, = the literal comma character
因此,您有一个搜索模式“拆分为至少一个空白字符和/或一个或多个逗号的字符串的任何部分”。
其他常见字符有:
. = any single character
* = any number of the preceeding character or group
^ (at start of pattern) = The start of the string
$ (at end of pattern) = The end of the string
^ (inside [...]) = "NOT" the following character
对于 PHP,the official documentation 中有很好的信息.
关于php - 如何在 PHP 中使用 preg_split()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24189698/