PHP - 从字符串中删除<img>标签

2022-08-30 09:03:58

嘿,我需要从字符串中删除所有图像,我只是找不到正确的方法。

这是我尝试过的,但它不起作用:

preg_replace("/<img[^>]+\>/i", "(image) ", $content);
echo $content;

有什么想法吗?


答案 1

尝试将 放在 前面的 。\>

编辑:我刚刚测试了你的正则表达式,它工作正常。这是我使用的:

<?
    $content = "this is something with an <img src=\"test.png\"/> in it.";
    $content = preg_replace("/<img[^>]+\>/i", "(image) ", $content); 
    echo $content;
?>

结果是:

this is something with an (image)  in it.

答案 2

您需要将结果赋回,因为不会修改原始字符串。$contentpreg_replace

$content = preg_replace("/<img[^>]+\>/i", "(image) ", $content);

推荐