PHP爆炸()模式是怎样的?

时间:2021-03-10 15:43:57

Say I have the following string:

假设我有以下字符串:

$str = "Hello, my name is Bob and I like 'pizza'. Do you like pizza??"

Currently I am able to split/explode this string on the whitespace using:

目前,我可以使用以下方法在空格处拆分/引爆这个字符串:

$arr = explode(' ', $str);

but I want to use the regex pattern \W like so:

但我想使用regex模式如下:

$arr = explode('\W', $str);

This should separate all words that aren't punctuation, allowing the 'pizza' part to be separated as pizza. Except it returns nothing (I get an empty array back).

这应该把没有标点符号的所有单词分开,让“披萨”部分被分离成披萨。但是它什么也不返回(我得到一个空数组)。

What can I do?

我能做什么?

Thanks

谢谢

3 个解决方案

#1


14  

Use preg_split:

使用preg_split:

http://www.php.net/manual/en/function.preg-split.php

http://www.php.net/manual/en/function.preg-split.php

#2


3  

explode does not do Regexen.
preg_split does.

爆炸不能重新生成。preg_split。

#3


1  

You should use preg_split instead of explode

您应该使用preg_split而不是爆炸。

#1


14  

Use preg_split:

使用preg_split:

http://www.php.net/manual/en/function.preg-split.php

http://www.php.net/manual/en/function.preg-split.php

#2


3  

explode does not do Regexen.
preg_split does.

爆炸不能重新生成。preg_split。

#3


1  

You should use preg_split instead of explode

您应该使用preg_split而不是爆炸。