php - 如何在 PHP 中使用 preg_split()?

标签 php preg-split

任何人都可以向我解释如何使用 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/

相关文章:

php - 在上传进度完成之前使 ajax 上传中止

PHP - call_user_function_array 或 Reflection 类通过引用传递?

php - 组合正则表达式以将驼峰式字符串拆分为单词

PHP 拆分为 preg_split()

PHP preg_split,按相同字符分割

php - 使用 PayPal PHP 接受简单的付款

php - jQuery 文件上传、获取和编辑保存在数据库中的数据

php - 为什么PHP set_execption_handler捕获错误对象

php - preg_match() 将一个字符串分割为另一个不包含撇号的字符串

PHP mb_split(),捕获定界符