【发布时间】:2013-04-14 19:00:40
【问题描述】:
例如,我想拆分这句话:
I am a sentence.
变成一个有5个部分的数组; I、am、a、sentence 和 .。
在尝试explode 之后,我目前正在使用preg_split,但我似乎找不到合适的东西。
这是我尝试过的:
$sentence = explode(" ", $sentence);
/*
returns array(4) {
[0]=>
string(1) "I"
[1]=>
string(2) "am"
[2]=>
string(1) "a"
[3]=>
string(8) "sentence."
}
*/
还有这个:
$sentence = preg_split("/[.?!\s]/", $sentence);
/*
returns array(5) {
[0]=>
string(1) "I"
[1]=>
string(2) "am"
[2]=>
string(1) "a"
[3]=>
string(8) "sentence"
[4]=>
string(0) ""
}
*/
如何做到这一点?
【问题讨论】:
标签: php regex string preg-split text-segmentation