【发布时间】:2013-03-28 02:53:51
【问题描述】:
我对@987654323@的使用有点困惑
我有一个$content,里面有一些网址。
我以前用过
$content = preg_match_all( '/(http[s]?:[^\s]*)/i', $content, $links );
foreach ($links[1] as $link ) {
// we have the link. find image , download, replace the content with image
// echo '</br>LINK : '. $link;
$url = esc_url_raw( $link );
$url_name = parse_url($url);
$url_name = $description = $url_name['host'];// get rid of http://..
$url = 'http://somescriptonsite/v1/' . urlencode($url) . '?w=' . $width ;
}
return $url;
但我真正需要的是用我解析的 URL 替换原始 URL...
所以我尝试了 preg_replace_callback:
function o99_simple_parse($content){
$content = preg_replace_callback( '/(http[s]?:[^\s]*)/i', 'o99_simple_callback', $content );
return $content;
}
和:
function o99_simple_callback($url){
// how to get the URL which is actually the match? and width ??
$url = esc_url_raw( $link );
$url_name = parse_url($url);
$url_name = $description = $url_name['host'];// get rid of http://..
$url = 'http://something' . urlencode($url) . '?w=' . $width ;
return $url; // what i really need to replace
}
我假设回调将以这样一种方式工作,即每个匹配项都会调用回调(递归?)并返回结果,从而允许使用解析的 $url 即时替换 $content 中的 URL来自o99_simple_callbac()。
但是这里的另一个question(尤其是this comment)引发了我的怀疑。
如果preg_replace_callback() 实际上传递了整个匹配数组,那么我之前使用的(第一个示例中的preg_match_all())与回调示例之间的实际区别是什么?
我错过了什么/误解了什么?
用解析后的 url 替换 $content 中的 URL 的正确方法是什么?
【问题讨论】:
标签: php callback preg-match-all preg-replace-callback