将文本拆分为带有特殊字符的单词

时间:2013-12-24 13:59:31

标签: php

我正在使用此代码将文本拆分为数组:

$wordArray = preg_split('#\PL+#u', $text, -1, PREG_SPLIT_NO_EMPTY);

Altough将google.com划分为google和com,以及什么是s和s。我必须管理preg_split对我来说是中文,你可以帮忙吗?

示例:

$text = "What's google.com if not the best website in the world?";
foreach ($wordArray as $w) {
    echo $w.'<br>';
}

What
s
google
com
if
not
the
best
website
in
the
world

What I want is

    What's
    google.com
    if
    not
    the
    best
    website
    in
    the
    world

2 个答案:

答案 0 :(得分:0)

如果您不致力于使用正则表达式,我个人会发现使用explode()函数拆分字符串会更容易。它仍然分裂成一个数组。

示例:

$wordArray = explode('.',$text);

以下是它的手册页:http://www.php.net/explode

答案 1 :(得分:0)

preg_split('/[\s]+/', $sentence, -1, PREG_SPLIT_NO_EMPTY);

使用 /[\s]+/ 正则表达式代替您正在使用的那个