小编Gun*_*uno的帖子

将句子分成单词

例如我有这样的哨兵:

$text = "word, word w.d. word!..";
Run Code Online (Sandbox Code Playgroud)

我需要这样的数组

Array
(
    [0] => word
    [1] => word
    [2] => w.d
    [3] => word".
)
Run Code Online (Sandbox Code Playgroud)

我是新来的正则表达式。

这是我尝试过的:

function divide_a_sentence_into_words($text){ 
    return preg_split('/(?<=[\s])(?<!f\s)\s+/ix', $text, -1, PREG_SPLIT_NO_EMPTY); 
}
Run Code Online (Sandbox Code Playgroud)

这个

$text = "word word, w.d. word!..";
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $text, -1, PREG_SPLIT_NO_EMPTY);
print_r($split);
Run Code Online (Sandbox Code Playgroud)

的作品,但我有第二个问题,我想在mu常规表达式中写列表“ wd”是特例。.例如,这句话是我的列表“ wd”,“ mr。”,“ dr。”

如果我要输入文字:

$ text =“单词,单词博士wd word!..”;

我需要数组:

Array (
  [0] => word
  [1] => dr.
  [2] => word
  [3] => w.d
  [4] => word 
)
Run Code Online (Sandbox Code Playgroud)

对不起,英语不好。

php text-segmentation

1
推荐指数
2
解决办法
1万
查看次数

标签 统计

php ×1

text-segmentation ×1