去除 PHP 中除一个单词以外的所有内容
Strip everything except one word in PHP
上下搜索,但找不到任何有用的东西。
我想删除除 you
.
以外的所有内容
$words = "you,us,them,our";
$keep = "you,";
这恰恰相反:
$words = str_replace("$keep", "", $words);
如何删除除 $keep
之外的所有内容?
str_replace(array('us', 'them', 'our', ','), array('','','',''), $words);
虽然这有点不好,当然。但如果你只有一些替代品,这将完成它的工作。当你想更换更多的东西时,它会变得丑陋。您可能想要添加一个模式并使用 preg_replace 和 not 运算符来去除除 'you' 之外的所有内容。
像这样:
$text = "You,I,He";
$matches = preg_replace("/[^you]/Uims", "", $text);
print_r($matches);
上下搜索,但找不到任何有用的东西。
我想删除除 you
.
$words = "you,us,them,our";
$keep = "you,";
这恰恰相反:
$words = str_replace("$keep", "", $words);
如何删除除 $keep
之外的所有内容?
str_replace(array('us', 'them', 'our', ','), array('','','',''), $words);
虽然这有点不好,当然。但如果你只有一些替代品,这将完成它的工作。当你想更换更多的东西时,它会变得丑陋。您可能想要添加一个模式并使用 preg_replace 和 not 运算符来去除除 'you' 之外的所有内容。
像这样:
$text = "You,I,He";
$matches = preg_replace("/[^you]/Uims", "", $text);
print_r($matches);