相关疑难解决方法(0)

将文本拆分为单个单词

我想用PHP将文本拆分成单个单词.你知道如何实现这个目标吗?

我的方法:

function tokenizer($text) {
    $text = trim(strtolower($text));
    $punctuation = '/[^a-z0-9äöüß-]/';
    $result = preg_split($punctuation, $text, -1, PREG_SPLIT_NO_EMPTY);
    for ($i = 0; $i < count($result); $i++) {
        $result[$i] = trim($result[$i]);
    }
    return $result; // contains the single words
}
$text = 'This is an example text, it contains commas and full-stops. Exclamation marks, too! Question marks? All punctuation marks you know.';
print_r(tokenizer($text));
Run Code Online (Sandbox Code Playgroud)

这是一个好方法吗?你有改进的想法吗?

提前致谢!

php split

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

标签 统计

php ×1

split ×1