global $url_path;
$url_path='abc.com';$html = preg_replace_callback("/(<a[^<]*\s+href\s*=\s*[\'\"])([^<]*)([\'\"])/is", "fun1", $html); function fun1($matches)
{
return $matches[1] .  $url_path(这里$url_path为何是空值?);
}问题,我要如何在回调函数里访问页面里的一个变量?

解决方案 »

  1.   

    function 内要 global 的啊!
        function fun1($matches)
        {
    global $url_path;
            return $matches[1] .  $url_path;//(这里$url_path为何是空值?);
        }
      

  2.   

    明白了,要先在函数里定义,然后再赋值,再调用    function fun1($matches)
        {
            global $url_path;
            return $matches[1] .  $url_path(这里$url_path为何是空值?);
        }$url_path='abc.com';$html = preg_replace_callback("/(<a[^<]*\s+href\s*=\s*[\'\"])([^<]*)([\'\"])/is", "fun1", $html);
      

  3.   

    global $url_path;
    $url_path='abc.com';
    $html = "<a href='/bbc/'>asdf</a>";
    $html = preg_replace_callback("/(<a[^<]*\s+href\s*=\s*)[\'\"]([^<]*)[\'\"]/is", "fun1", $html);    function fun1($matches)
        {
         global $url_path;
            return $matches[1] .  $url_path;
        }
    echo $html;