从 URL 创建 Img 标签

发布于 2024-09-12 15:59:04 字数 833 浏览 5 评论 0原文

我想要什么


如果字符串中的 URL 在 URL 末尾(不是字符串)包含 .jpg,那么它应该生成一个使用 preg_replace 从中获取图像,否则创建一个正常链接。

例如:

如果我有 http://www.example.com/images/photo.jpg 那么它应该替换为:

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));

What I want


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.

so for example:

If I have http://www.example.com/images/photo.jpg then it should replace with:

<img src="http://www.example.com/images/photo.jpg" alt="http://www.example.com/images/photo.jpg">

The problem:


The URL is replaced with a link in any way and my regex isn't working :( .

What I have tried:


        $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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岛歌少女 2024-09-19 15:59:04

试试这个

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;
}

Try this

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;
}
溇涏 2024-09-19 15:59:04
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
$content = preg_replace('#\b(http://\S+\.jpg)\b#i', '<img src="$1" alt="$1" />', $content);
黎夕旧梦 2024-09-19 15:59:04

你不需要东张西望。就跟着去

$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);    

You don't need lookaround. Just go with

$content = preg_replace("#(http://[^ ]+\\.jpg(?= |$)#i","<img src=\"$1\" alt=\"$1\"/>", $content);    
老娘不死你永远是小三 2024-09-19 15:59:04

我认为当您想要向后查找时,您使用了向前查找运算符。您可以将 (?=\.jpg) 更改为 (?<=\.jpg) 但还有其他更干净的正则表达式,我相信其他人会发布。

I think you used the lookahead operator when you wanted lookbehind. You could change (?=\.jpg) to (?<=\.jpg) but there are other, cleaner regex's I'm sure others will post.

怼怹恏 2024-09-19 15:59:04

这对我有用。

$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg

在上图中,路由器 R1 有两个点到点。 ';

$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);

echo $parse_img;

苏亚什

This worked for me.

$parse_img='Hello, http://orbitco-ccna-pastquestions.com/images/Q5.jpg

In the figure above, router R1 has two point-to-point . ';

$parse_img=preg_replace('/(https?:\/\/(.\*)?\\.jpg|png|gif)[\s+]*/i',"< img src=\"$1\" alt = \"$1\">< /img >",$parse_img);

echo $parse_img;

Suyash

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文