除了PHP中的一个单词之外的所有内容

时间:2018-05-07 01:07:32

标签: php preg-replace str-replace

上下搜索,但我找不到任何有用的东西 我想删除除checkTenantName() { this.taken = true; console.log(this.taken); } 之外的所有内容。

.taken

这恰恰相反:

you

如何删除$words = "you,us,them,our"; $keep = "you,"; 以外的所有内容?

1 个答案:

答案 0 :(得分:0)

str_replace(array('us', 'them', 'our', ','), array('','','',''), $words);

虽然这有点不好,当然。但是,如果你只有一些替代品,这将完成它的工作。当你想要更换更多的东西时,它会变得丑陋。您可能想要添加一个模式并使用preg_replace和not运算符去除除“你”之外的所有内容。

像这样:

$text = "You,I,He";
$matches = preg_replace("/[^you]/Uims", "", $text);

print_r($matches);