Preg 回调问题
伟大的伙伴们... 我需要你的帮助..请让我知道为什么这段代码没有进入回调函数...
wppp_content_hook('<p><a href="http://localhost/work/Kate/wp-content/uploads/2011/05/amg_sls_01.jpg"><img width="300" height="250" class="alignleft size-medium wp-image-195" title="amg_sls_0" alt="" src="http://localhost/work/Kate/wp-content/uploads/2011/05/amg_sls_01-300x250.jpg"></a></p>');
function wppp_content_hook($content) {
$fileTypes = array('\.bmp|\.gif|\.jpg|\.jpeg|\.png');
$pattern = array();
$rel = 'wp-prettyPhoto';
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)>/i";
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)(rel=('|\")".$rel."(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")".$rel."(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";
return preg_replace_callback($pattern,'_regexCallback',$content);
}
function _regexCallback($matches) {
echo "<pre>";
print_r($matches);
echo "</pre>";
global $post;
$fileTypes = array('\.bmp|\.gif|\.jpg|\.jpeg|\.png');
$rel = 'wp-prettyPhoto';
$pattern = array();
$replacement = array();
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)>/i";
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)(rel=('|\")".$rel."(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")".$rel."(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";
$replacement[] = '<a$1href=$2$3$4$5$6 rel="'.$rel.'[g'.$post->ID.']">';
$replacement[] = '<a$1href=$2$3$4$5$6$7>';
return !strpos($matches['0'], '_blank')? preg_replace($pattern,$replacement,$matches['0']) : $matches['0'];
}
Great Mates...
I need your help.. please let me know why this code is not getting in to the callback function...
wppp_content_hook('<p><a href="http://localhost/work/Kate/wp-content/uploads/2011/05/amg_sls_01.jpg"><img width="300" height="250" class="alignleft size-medium wp-image-195" title="amg_sls_0" alt="" src="http://localhost/work/Kate/wp-content/uploads/2011/05/amg_sls_01-300x250.jpg"></a></p>');
function wppp_content_hook($content) {
$fileTypes = array('\.bmp|\.gif|\.jpg|\.jpeg|\.png');
$pattern = array();
$rel = 'wp-prettyPhoto';
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)>/i";
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)(rel=('|\")".$rel."(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")".$rel."(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";
return preg_replace_callback($pattern,'_regexCallback',$content);
}
function _regexCallback($matches) {
echo "<pre>";
print_r($matches);
echo "</pre>";
global $post;
$fileTypes = array('\.bmp|\.gif|\.jpg|\.jpeg|\.png');
$rel = 'wp-prettyPhoto';
$pattern = array();
$replacement = array();
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)>/i";
$pattern[] = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)(rel=('|\")".$rel."(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")".$rel."(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";
$replacement[] = '<a$1href=$2$3$4$5$6 rel="'.$rel.'[g'.$post->ID.']">';
$replacement[] = '<a$1href=$2$3$4$5$6$7>';
return !strpos($matches['0'], '_blank')? preg_replace($pattern,$replacement,$matches['0']) : $matches['0'];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您的
$pattern
与$content
不匹配,精确查看您的实际正则表达式
通过
var_export
'ing$pattern
回调 仅当您的内容与正则表达式匹配时才会执行。否则没有理由执行回调函数,因为没有匹配项。实际正则表达式的一部分:
([A-Za-z0-9\\/_\\.\\~\\:-]*?)(Array)
似乎不需要的“数组”潜入了您的模式(他们俩)。
Because your
$pattern
does not match$content
Look precisely at your actual regexps by
var_export
'ing$pattern
Callback will only be executed if your content will be matched by regular expression. No reason to execute callback function otherwise since there are no matches.
Part of your actual regex:
([A-Za-z0-9\\/_\\.\\~\\:-]*?)(Array)
It seems like not desired "Array" sneaked into your pattern (both of them).