嘿,我正在尝试做2 preg_replace:1.make所有url到html链接2.make所有图像url到html img标签
但是这些正则表达式取消了另一个
<?php
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exImg = "!http://[a-z0-9\-\.\/]+\.(?:jpe?g|png|gif)!Ui";
// The Text you want to filter for urls
$text = "The text you want to filter goes here. http://google.comhttp://www.ynet.co.il http://dogsm.files.wordpress.com/2011/12/d7a1d7a7d795d791d799-d793d795.jpg";
// Check if there is a url in the text
$text = preg_replace($reg_exImg, '<img src=$0 >', $text);
$text = preg_replace($reg_exUrl, '<a href="$0" rel="nofollow">$0</a>', $text);
echo $text;
?>
如何使链接网址的preg_replace不会对标签执行此操作?
谢谢。
海姆
作为回调可能会更好。
仅使用$reg_exUrl
,然后执行以下操作:
$text = preg_replace_callback($reg_exUrl,function($m) {
if( preg_match("/\.(?:jpe?g|png|gif)$/i",$m[0])) {
return "<img src='".$m[0]."' />";
}
else {
return "<a href='".$m[0]."' rel='nofollow'>".$m[0]."</a>";
}
},$text);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句