我想要的是
如果字符串中的 URL 包含.jpg
at the 网址末尾(不是字符串)那么它应该用它来制作图像preg_replace
否则建立一个正常的链接。
例如:
如果我有http://www.example.com/images/photo.jpg
那么它应该替换为:
<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">
问题:
URL 以任何方式替换为链接,并且我的正则表达式不起作用:( 。
我尝试过的:
$content = preg_replace("/(http:\/\/[^\s]+(?=\.jpg))/i","<img src=\"$1\" alt = \"$1\"></img>",$content);
$content = nl2br(preg_replace("/(http:\/\/[^\s]+(?!\.jpg))/m", "<a href=\"$1\" rel=\"nofollow\" target=\"blank\" title=\"$1\" class=\"news-link\">$1</a>", $content));
尝试这个
function replace_links($content)
{
if (preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content))
{
$content = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<img src="$1.$2" alt="$1.$2" />', $content);
}
else
{
$content = preg_replace('#(http://[^\s]+(?!\.(jpe?g|png|gif)))#i', '<a href="$1" rel="nofollow" target="blank" title="$1" class="news-link">$1</a>', $content);
}
return $content;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)