从URL创建Img标记 [英] Create Img tag from URL
本文介绍了从URL创建Img标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要的是什么
如果字符串中的网址包含 .jpg
在 URL的末尾(不是字符串)然后它应该使用 preg_replace $ c从中制作图像$ c>否则建立正常链接。
If the URL in the string contains a .jpg
at the end of the URL (not the string) then it should make an image from it with preg_replace
else make a normal link.
所以例如:
如果我有 http://www.example.com/images/photo.jpg
然后它应替换为:
If I have http://www.example.com/images/photo.jpg
then it should replace with:
< img src =http://www.example.com/images/photo.jpgalt =http://www.example.com/images/photo.jpg>
问题:
URL以任何方式替换为链接,我的正则表达式无效:(。
The URL is replaced with a link in any way and my regex isn't working :( .
我尝试过的方法:
$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;
}
这篇关于从URL创建Img标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文