0

对于这段代码,有谁知道为什么只$unuseful替换字符串中的第一个单词?

$unuseful = array(" is ", " the ", " for ", " and ", " with "," that ", " this "," or ", ",",";","/","?","!",".");
$aux = str_replace($unuseful, " " , $statement);

"dude cost that free the dude the "-> dude cost free dude the

“dude cost that free the dude the the”-> dude cost free dude the the

提前致谢!

4

3 回答 3

4

那是因为您放置了前导和尾随空格。

它替换了“ the ”一次,因为“ the ”只有 3 个空格,而您正在寻找替换 4 个空格。

于 2012-06-26T08:32:39.390 回答
2
    for($i=0;$i<count($unuseful);$i++)
    {$statement = str_replace($unuseful[$i], " " , $statement);}
$aux=$statement;
于 2012-06-26T08:33:43.030 回答
1
<?php
$unuseful = array('/\s+is\s+/', '/\s+the\s+/', '/\s+for\s+/', '/\s+and\s+/', '/\s+with\s+/','/\s+that\s+/', '/\s+this\s+/','/\s+or\s+/', '/,/','/;/','/\//','/\?/','/\!/','/\./');

$challenge = 'dude cost that free the dude the the';
echo preg_replace($unuseful, ' ', $challenge)."\n";
?>

给出:

花花公子免费花花公子

于 2012-06-26T09:06:37.387 回答