dragonpeng200811111 2013-07-11 13:06
浏览 10

替换描述中的图像的src

I have a string coming from database as given below. I want to change the img src in the text below to be ./upload-files/<filename> instead of ../upload-files/<filename>.

<p><span style="text-decoration: line-through;">
<img style="float: left; margin-left: 5px; margin-right: 5px;" src="../upload-files/teddybear.jpg">
</span></p>

So basically I want image to use the current directory instead of the parent directory ../ How can i do this? Is preg_replace the right tool here?

  • 写回答

1条回答 默认 最新

  • dpftppc9674 2013-07-12 06:21
    关注

    If you are using an intermediary variable to store your image you can use:

    $img_url = '../upload-files/teddybear.jpg';
    
    $img_url = print_r(preg_replace('/^\.\./', '.', $img_url));
    
    <p><span style="text-decoration: line-through;">
    <img style="float: left; margin-left: 5px; margin-right: 5px;" src="<?php echo $img_url;?>">
    </span></p>
    

    if you are trying to replace ../ inside a bunch of html, use this:

    $my_html = '<p><span style="text-decoration: line-through;">
    <img style="float: left; margin-left: 5px; margin-right: 5px;" src="../upload-files/teddybear.jpg">
    </span></p>';
    
    print_r(preg_replace('/"\.\./', '".', $my_html));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题