2014-03-13 106 views
0

我有一个像变更网址与其他网址笨

$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>..... 
     ..................................."; 

我想更换网址,像如下的超级链接字符串,

$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> 
      .................................................. 
      ..............................."; 

我的意思是,每个URL会是一个段我的网络url.Is可能用了preg_replace(),请帮我

回答

1

尝试用str_replace

$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; 

加评论,如果网址是动态的,那么这些阵列将像

$arr1 = array($url1,$url2,$url3); 
$arr2 = array('http://my_url.com/'.$url1,'http://my_url.com/'.$url2,'http://my_url.com/'.$url3); 

或者干脆放弃使用

$newStr = str_replace("href='" , "href='http://my_url.com/" , $str); 
+0

的URL是动态创建一个尝试,我不知道它们是什么。这只是一个例子 – Shin

+0

考虑我的编辑一次。你只需要创建与该动态URL – Gautam3164

+0

阵列但我不知道如何把所有的URL到$ arr1.because该字符串contail很多网址 – Shin