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

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(.....));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
    • ¥18 如何用c++编写数学规律题
    • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
    • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
    • ¥20 怎样通过一个网址找到其他同样模版的网址
    • ¥30 XIAO esp32c3 读取FDC2214的数据
    • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信
    • ¥15 关于PROCEDURE和FUNCTION的问题
    • ¥100 webapi的部署(标签-服务器)
    • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)