preg_replace_callback为何无法访问全局的变量?

110来电 2009-09-03 10:47:54

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为何是空值?);
}

问题,我要如何在回调函数里访问页面里的一个变量?
...全文
141 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
110来电 2009-09-06
  • 打赏
  • 举报
回复
请指出
江南昆虫 2009-09-04
  • 打赏
  • 举报
回复
function 内要 global 的啊!
    function fun1($matches)
{
global $url_path;
return $matches[1] . $url_path;//(这里$url_path为何是空值?);
}
程序猿之殇 2009-09-04
  • 打赏
  • 举报
回复
正则写的也有点问题
程序猿之殇 2009-09-04
  • 打赏
  • 举报
回复
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;
110来电 2009-09-04
  • 打赏
  • 举报
回复
明白了,要先在函数里定义,然后再赋值,再调用

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);


21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧