【问题标题】:preg_replace_callback - do twicepreg_replace_callback - 做两次
【发布时间】:2010-11-18 07:12:27
【问题描述】:

哟,我正在尝试使这个脚本工作,但它不起作用。我怎么做两次,preg_replace_callback 有两个不同的功能。谢谢!

 function prepend_proxy($matches) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend = $matches[2] ? $matches[2] : $url;
    $prepend = 'proxy2.php?url='. $prepend .'/';

    return $matches[1] . $prepend . $matches[3];
}

function imgprepend_proxy($matches2) {
   $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}";
    $prepend2 = $matches2[2] ? $matches2[2] : $url;
    $prepend2 = $prepend2 .'/';

    return $matches2[1] . $prepend2 . $matches2[3];
}


$new_content = preg_replace_callback(
    '|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
    'prepend_proxy',
    '|(src=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
    'imgprepend_proxy',
    $content
); 

【问题讨论】:

    标签: php regex preg-replace-callback


    【解决方案1】:
    $new_content = preg_replace_callback(
        '|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
        'prepend_proxy',
        preg_replace_callback(
            '|(src=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
            'imgprepend_proxy',
            $content
        )
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多