移除& # 39;词# 39;包含在非字母数字字符的字符串中


Removing 'words' contained in strings with non-alphanumeric characters?

在PHP中删除非字母数字字符字符串中的'words'的推荐方法是什么?

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

预期的结果:

"Test test 123. EOTest.";

方法1 - regex方法2 - explosion (), foreach()和str_replace或preg_replace

尝试使用preg_split, preg_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.

我希望这对你有帮助!