0

我通过 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_pluginMY NEW PATH.

如何使用 php 替换字符串?

4

2 回答 2

0

str_replace

请使用谷歌,网上有成千上万个该问题的答案。:)

此外,如果您想在这样的 URL 中嵌入链接:

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

您需要在传递的 URL 上使用urlencode&q=

所以基本上:

$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
于 2013-02-19T10:20:45.487 回答
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);
}
于 2013-02-19T10:20:49.153 回答