create_function() 到 function() 解析错误:语法错误,

问题描述 投票:0回答:1

我正在将 creat_function 转换为 PHP 8.0 的函数。我只是需要一双新的眼睛来看待这个问题。我看不出“}”问题出在哪里。有人看到吗?否则代码应该正确工作吗?

function roots_root_relative_url($input) {
    $output = preg_replace_callback(
        '!(https?://[^/|"]+)([^"]+)?!',
        function($matches){
            // if full URL is site_url, return a slash for relative root
            'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' .
            // if domain is equal to site_url, then make URL relative
            '} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' .
            // if domain is not equal to site_url, do not make external link relative
            '} else { return $matches[0]; };'
        
        } **< OFFENDING BRACKET**
        
        $input
    );
    return $output;
}

我删除了“}”,但随后我得到“解析错误:语法错误,第 56 行目录中出现意外的“$input”(T_VARIABLE)”我到底错过了什么???

php function parse-error create-function
1个回答
0
投票
function roots_root_relative_url($input) {
    
    $output = preg_replace_callback(
        
        '!(https?://[^/|"]+)([^"]+)?!',
        
        function($matches){
            
            // if full URL is site_url, return a slash for relative root
            'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' .
            // if domain is equal to site_url, then make URL relative
            '} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' .
            // if domain is not equal to site_url, do not make external link relative
            '} else { return $matches[0]; };';
        
        },
        
        $input
    );
    return $output;
}
© www.soinside.com 2019 - 2024. All rights reserved.