douyong1885 2017-12-17 17:22
浏览 405
已采纳

str_replace()无效

for the first time I encounter such a problem. It works;

function translate($google) {
$en = array(
    "Mother", "Father"
 );
 $de= array(
    "Mutter", "Vater" 
 );
$s = str_replace($en,$de,$google);
return $s;}

But this does not work

$en = array(
        "Mother", "Father"
     );
     $de= array(
        "Mutter", "Vater" 
     );
function translate($google, $en, $de) {
    $s = str_replace($en,$de,$google);
    return $s;
}

where do i make mistakes?

Last time I'll use it like this;

echo translate(fonkfonk(str_replace(array("
","","\t"),array("‌​","",""),file_get_co‌​ntents($cache))));
  • 写回答

1条回答 默认 最新

  • drox90250557 2017-12-17 17:31
    关注

    Your problem is that you're not providing the values of $en and $de to your function when calling it.

    $en = array("Mother", "Father");
    $de = array("Mutter", "Vater");
    function translate($google, $en, $de) {
        $s = str_replace($en,$de,$google);
        return $s;
    }
    
    echo translate(fonkfonk(.....)); // error because translate() does not know what
                                     // $en and $de are supposed to be
    

    You are only providing the result of the fonkfonk() function as the first argument ($google) and not providing a second and third argument.

    What you should do is either provide the values of $en and $de in your function call, or import them when you define the function:

    function translate($google, $en, $de) {
        $s = str_replace($en,$de,$google);
        return $s;
    }
    
    $en = array("Mother", "Father");
    $de = array("Mutter", "Vater");
    echo translate(fonkfonk(.....), $en, $de);
    

    Or:

    $en = array("Mother", "Father");
    $de = array("Mutter", "Vater");
    function translate($google) use ($en, $de) {
        $s = str_replace($en,$de,$google);
        return $s;
    }
    
    echo translate(fonkfonk(.....));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况