用php替换字符串中的特定URL

时间:2021-08-05 16:49:19

A string contains various urls like this:

一个字符串包含各种url,如下所示:

http://www.example.com/viewtopic.php?f=36&t=457
http://www.example.com/viewtopic.php?f=36&t=11782

Example with 1 URL:

1个URL的示例:

Lorem ipsum dolor sit amet, http://www.example.com/viewtopic.php?f=36&t=457 consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.

Example with 2 URLs

有2个URL的示例

Lorem ipsum dolor sit amet, http://www.example.com/viewtopic.php?f=36&t=457 consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. http://www.example.com/viewtopic.php?f=36&t=11782 At vero eos et accusam et justo duo dolores et ea rebum.

The URLs inside the text must be replaced with new seo urls.

必须使用新的seo URL替换文本中的URL。

http://www.example.com/viewtopic.php?f=36&t=457

becomes

http://www.example.com/this-ist-the-new-url.html

The Seo URLs will be generatred by a function.

Seo URL将由函数生成。

How is it possible to search and replace the FULL url with a other URL inside a string when the URL contains this:

当URL包含以下内容时,如何使用字符串中的其他URL搜索和替换FULL url:

http://www.example.com/viewtopic.php

Thank you very much.

非常感谢你。

1 个解决方案

#1


0  

The function below is looking for a perfect match in a string. It doesn't matter if something is there before or after the string as long as the given characters are there in order.

下面的函数是在字符串中寻找完美匹配。只要给定的字符按顺序存在,在字符串之前或之后有什么内容都没关系。

$phrase  = "Whatever you write here it can be replaced";
$replacethis = array("you", "can", "replaced");
$withtis   = array("I", "can't", "placed");

$newphrase = str_replace($replacethis, $whithis, $phrase);

And $newphrase would be: Whatever I write here it can't be placed

$ newphrase将是:无论我在这里写什么,它都无法放置

In your case it would be:

在你的情况下,它将是:

$phrase  = "Whatever you write here it can be replaced";
$replacethis = array("http://www.example.com/viewtopic.php?f=36&t=457");
$withtis   = array("http://www.example.com/this-ist-the-new-url.html");

$newphrase = str_replace($replacethis, $whithis, $phrase);

#1


0  

The function below is looking for a perfect match in a string. It doesn't matter if something is there before or after the string as long as the given characters are there in order.

下面的函数是在字符串中寻找完美匹配。只要给定的字符按顺序存在,在字符串之前或之后有什么内容都没关系。

$phrase  = "Whatever you write here it can be replaced";
$replacethis = array("you", "can", "replaced");
$withtis   = array("I", "can't", "placed");

$newphrase = str_replace($replacethis, $whithis, $phrase);

And $newphrase would be: Whatever I write here it can't be placed

$ newphrase将是:无论我在这里写什么,它都无法放置

In your case it would be:

在你的情况下,它将是:

$phrase  = "Whatever you write here it can be replaced";
$replacethis = array("http://www.example.com/viewtopic.php?f=36&t=457");
$withtis   = array("http://www.example.com/this-ist-the-new-url.html");

$newphrase = str_replace($replacethis, $whithis, $phrase);