Php 如何识别图像URL并用img标记替换它们?

Php 如何识别图像URL并用img标记替换它们?,php,Php,我从数据库中获取字符串,其中可以包含一些图像的URL。但是我不希望用户看到写在那里的URL,而是实际的图像。我曾尝试在谷歌和stackoverflow上搜索,但我得到的答案要么不相关,要么难以理解 比如说 $string= "hello everyone check this picture: http://somesite.com/heretheimage.jpg"; 之后: $new_string = "hello everyone check this picture: <img

我从数据库中获取字符串,其中可以包含一些图像的URL。但是我不希望用户看到写在那里的URL,而是实际的图像。我曾尝试在谷歌和stackoverflow上搜索,但我得到的答案要么不相关,要么难以理解

比如说

$string= "hello everyone check this picture: http://somesite.com/heretheimage.jpg";
之后:

$new_string = "hello everyone check this picture: <img src='http://somesite.com/heretheimage.jpg'>";
$new\u string=“大家好,请检查此图片:”;
如何在php中实现这一点

编辑:有人将这些问题标记为可能的重复问题。但我想告诉你们,在这个问题上,用户想要识别URL。但在我的问题中,它特别适用于图像URL,然后这些URL应该包含在img标记中。

您可以使用函数尝试以下操作:

    <?php
    $string= "hello everyone check this picture: http://somesite.com/heretheimage.jpg";
    $pattern = '/(?:(?:https?:\/\/))[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/=]*(\.jpg|\.png|\.jpeg))/';
    preg_match($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
    $new_string = "hello everyone check this picture: <img src='".$matches."'>";
    ?>

可能重复“但我得到的答案要么不相关,要么难以理解”-它们是什么?您尝试过什么吗?您的db设计也很糟糕。顺便说一句,你留下问题了吗?