Reputation: 7768
I have a string like
$str="<a href='https://www.google.com'>Google</a>Test url
<a href='https://www.twitter.com'>Twitter</a>Sample content
<a href='example.com'>Example</a>.....
...................................";
I want to replace the urls in the hyperlink like as follows,
$result="<a href='http://my_url.com/https://www.google.com'>Google</a>Test url
<a href='http://my_url.com/https://www.twitter.com'>Twitter</a>Sample content
<a href='http://my_url.com/example.com'>Example</a>
..................................................
...............................";
I mean,Every url will be a segment of my web url.Is possible with a preg_replace(),Please help me
Upvotes: 0
Views: 60
Reputation: 28763
Try with str_replace
like
$arr1 = array('https://www.google.com','https://www.twitter.com','example.com');
$arr2 = array('http://my_url.com/https://www.google.com','http://my_url.com/https://www.twitter.com','http://my_url.com/example.com');
$str="<a href='https://www.google.com'>Google</a>Test url
<a href='https://www.twitter.com'>Twitter</a>Sample content
<a href='example.com'>Example</a>";
$newStr = str_replace($arr1 , $arr2 , $str);
echo $newStr;
Acc to comment if the urls are dynamic then those arrays will be like
$arr1 = array($url1,$url2,$url3);
$arr2 = array('http://my_url.com/'.$url1,'http://my_url.com/'.$url2,'http://my_url.com/'.$url3);
Or simply give a try with
$newStr = str_replace("href='" , "href='http://my_url.com/" , $str);
Upvotes: 1