duangai9678 2013-07-17 09:22
浏览 35

相对于字符串中包含的绝对链接的更改

I have PHP variable holding string, within i have a mix of contents having links and other containing image links, like shown below

$baseurl = "http://www.myweb.com/home/";
$content = "<a href="#"><img src="images/comment.gif" alt="Comment" /></a></p>
           <div class="thirds"> <p><b><a  href="admin/local.html" class="title">Manage your content</a></b><br /><img src ="subfolder/home.jpg" alt="home" />Lorem ipsum dolor sit amet esta pa, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt....";

My Target is to echo variable 'content' having all links with a tag, src tag, modified to absolute meaning, for the first $content link contained has to be <a href="http://www.myweb.com/home/#" this should be the same to all images example for <img src="images/comment.gif should be <img src="http://www.myweb.com/home/images/comment.gif alt="Comment" />.. Any help please, note, all contents are contained in a php string variable.

  • 写回答

3条回答 默认 最新

  • duanpo7796 2013-07-17 09:25
    关注

    You can prepend the $baseurl to the src. Also change the " to ' so you can use " inside the string. Or better if you use a heredoc.

    $baseurl = "http://www.myweb.com/home/";
    $content = '<a href="'. $baseurl . '#"><img src="'. $baseurl . 'images/comment.gif" alt="Comment" /></a></p>
           <div class="thirds"> <p><b><a  href="admin/local.html" class="title">Manage your content</a></b><br /><img src ="'. $baseurl . 'subfolder/home.jpg" alt="home" />Lorem ipsum dolor sit amet esta pa, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt....';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题