2013-02-19 57 views
0

我通過foreach循環獲得了一些值。現在我想用一個字符串檢查每個值。找到並替換php中的字符串

我的字符串是"http://www.example.com/TantraProjects/Ranjit/nt_plugin"

我喜歡寫在下面的值:

http://blogsearch.google.com/blogsearch?scoring=d&partner=wordpress&q=link:http://www.example.com/TantraProjects/Ranjit/nt_plugin/ 

http://blogsearch.google.com/blogsearch_feeds?scoring=d&ie=utf-8&num=10&output=rss&partner=wordpress&q=link:http://www.example.com/TantraProjects/Ranjit/nt_plugin/ 

link:http://www.example.com/TantraProjects/Ranjit/nt_plugin/ - Google Blog Search 

現在我想改變這一切的價值觀,與MY NEW PATH更換http://www.example.com/TantraProjects/Ranjit/nt_plugin

我該如何使用php替換字符串?

回答

0

str_replace

而且請使用谷歌,有數以千計的這個問題的答案在網上。 :)

此外,如果你要嵌入的鏈接在這樣的網址:

http://blogsearch.google.com/blogsearch?scoring=d&partner=wordpress&q=link:http://www.example.com/TantraProjects/Ranjit/nt_plugin/

你需要在你&q=後通過URL使用urlencode

所以基本上:

$string = "http://blogsearch.google.com/blogsearch?scoring=d&partner=wordpress&q=MY_NEW_PATH"; 
$output = str_replace('MY_NEW_PATH', "link:".urlencode('http://www.example.com/TantraProjects/Ranjit/nt_plugin'), $string); //$search , $replace, $subject 
0

可能通過使用str_replace這樣的:

$findstr = "http://www.example.com/TantraProjects/Ranjit/nt_plugin"; 
$newstr = "new string here"; 
$newlist = array(); 

foreach($list as $item) 
{ 
    $newlist[] = str_replace($findstr, $newstr, $item); 
}