将句子分成单词 [英] Split sentence into words
本文介绍了将句子分成单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有这样的哨兵:
for example i have sentenes like this:
$text = "word, word w.d. word!..";
我需要这样的数组
Array
(
[0] => word
[1] => word
[2] => w.d
[3] => word".
)
我是新来的正则表达式.
I am very new for regular expression..
这是我尝试过的:
function divide_a_sentence_into_words($text){
return preg_split('/(?<=[\s])(?<!f\s)\s+/ix', $text, -1, PREG_SPLIT_NO_EMPTY);
}
这个
$text = "word word, w.d. word!..";
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($split);
可行,但是我还有第二个问题我想用mu正则表达式写列表 "w.d"是特例.例如,这是我的列表"w.d","mr.","dr."
works, but i have second question i want to write list in mu regular exppression "w.d" is special case.. for example this words is my list "w.d" , "mr.", "dr."
如果我要输入文字:
$ text =单词,单词博士w.d. word!..";
$text = "word, dr. word w.d. word!..";
我需要数组:
Array (
[0] => word
[1] => dr.
[2] => word
[3] => w.d
[4] => word
)
抱歉英语不好...
推荐答案
使用正则表达式为/[^\w]*([\s]+[^\w]*|$)/
的preg_split
应该可以正常工作:
Using preg_split
with a regex of /[^\w]*([\s]+[^\w]*|$)/
should work fine:
<?php
$text = "word word w.d. word!..";
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($split);
?>
输出:
Array
(
[0] => word
[1] => word
[2] => w.d
[3] => word
)
这篇关于将句子分成单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文