douao1854 2017-11-27 02:31
浏览 42

错误将图像标记替换为图像src

$content = 'This fairy tale-like place is based on a story of two lovers who ran away to escape their enemies.
<img src="blisshouse-1.jpg"> <img src="blisshouse-2.jpg">';

preg_match_all('%<img.*?src=["\'](.*?)["\'].*?/>%i', $content, $matches);
if(count($matches) > 0) {
    foreach ($matches as $matche) {
        $content = preg_replace('%<img.*?src=["\'](.*?)["\'].*?/>%i', $matche[1], $content);
    }
}
echo $content;

Result is:

This fairy tale-like place is based on a story of two lovers who ran away to escape their enemies.
blisshouse-1.jpg blisshouse-1.jpg

Error can't replace <img src="blisshouse-2.jpg"> to blisshouse-2.jpg. How to fix it

  • 写回答

1条回答 默认 最新

  • douke1954 2017-11-27 03:47
    关注

    As @Mario suggested - just use preg_replace

    Also, I added a /? because the img tags didn't have slashes.

    $content = 'This fairy tale-like place is based on a story of two lovers who ran away to escape their enemies.
    <img src="blisshouse-1.jpg"> <img src="blisshouse-2.jpg">';
    
    $content = preg_replace('%<img.*?src=["\'](.*?)["\'].*?/?>%i', '$1', $content);
    echo $content;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现