2014-07-09 60 views
0

当我尝试使用str_replace创建用户定义函数时,它没有任何作用。当不在用户定义的函数中时,相同代码的组件完美工作。以下显示了我的问题的简单表示。PHP str_replace()在用户定义函数中不起作用

<?php 
function replacelist($thetext) { 
    $seachfor = array("crackers","soup"); 
    $replacewith = array("shapes","stew"); 
    $newtext = str_replace($searchfor, $replacewith, $thetext); 
    return $newtext." - plus some test text"; 
    } 
?> 


<?php 
$mytext="Animal crackers in my soup"; 

$newphrase = replacelist($mytext); 

echo $newphrase; 
?> 

动物饼干上述结果在我的汤 - 再加上一些测试文本

任何帮助感激地接受。

感谢大卫

+4

激活错误报告!它会告诉你,你已经打错了你的变量名。 – deceze

+0

TYPO - $ seachfor和$ searchfor – tiriana

+0

通常复制/粘贴变量以避免拼写错误是一个好主意。 – Konst

回答

3

拼写正确$seachfor,它会工作。

+0

这是尴尬。我现在有更复杂的版本。 - 非常感谢您的帮助 –