【问题标题】:Removing 'words' contained in strings with non-alphanumeric characters?删除包含非字母数字字符的字符串中的“单词”?
【发布时间】:2016-06-25 03:34:25
【问题描述】:

请问在 PHP 中删除包含非字母数字字符的字符串中的“单词”的推荐方法是什么?

$string = "Test let's test 123. https://youtu.be/dQw4w9WgXcQ EOTest.";

想要的结果:

"Test test 123. EOTest.";

方法 1 - 正则表达式 方法 2 - explode()、foreach() 和 str_replace 或 preg_replace

【问题讨论】:

  • 使用分解功能

标签: php regex nlp


【解决方案1】:

尝试使用preg_splitpreg_grepimplode 函数,如下所示:

$string = "Test let's test 123. https://youtu.be/dQw4w9WgXcQ EOTest.";
$words = preg_split('/\s+/', $string); // split on one or more spaces
$filter = preg_grep('/^[A-Za-z\d.]+$/', $words); // allow dot, letters, and numbers
$result = implode(' ', $filter); // turn it into a string
print_r($result); // -> Test test 123. EOTest.

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多