在php中查找并替换字符串

时间:2013-02-19 10:18:32

标签: php

我通过foreach循环获得了许多值。现在我想用字符串检查每个值。

我的字符串是"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

现在,我想更改所有值,将http://www.example.com/TantraProjects/Ranjit/nt_plugin替换为MY NEW PATH

如何使用php替换字符串?

2 个答案:

答案 0 :(得分:0)

<强> str_replace

请使用谷歌,在线有数千个问题的答案。 :)

此外,如果您想在以下网址中嵌入链接:

  

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

您需要在&q=之后传递的网址上使用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

答案 1 :(得分: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);
}