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

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.

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....';

try this (quick and dirty way but might be faster than regexp):

str_replace("href=\"", "href=\"".$baseurl, $content);
str_replace("href=\'", "href=\'".$baseurl, $content);
str_replace("src=\"", "src=\"".$baseurl, $content);
str_replace("src=\'", "src=\'".$baseurl, $content);

You have 2 ways:

  1. Server side, by php, replace string with regular expression(I've no idea).

  2. Browser side, by jquery:

    var baseurl = "http://www.myweb.com/home/";
    $("#container [src]").prop("src",function(index,oldValue){
        return baseurl+oldValue;
    });
    
    $("#container [href]").prop("href",function(index,oldValue){
        return baseurl+oldValue;
    });
    

It's easy to put more properties into an array and execute them in a for loop.

"container" is the id of the dom element which contains your content. It's like this:

<div id="container">
   <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 s...
</div>