Php 如何使用preg_replace选择URL并将其更改为文本

Php 如何使用preg_replace选择URL并将其更改为文本,php,regex,Php,Regex,我有这样一个文本: Tapez ici le message que vous souhaitez envoyer à vos clients http://go.tanger.fr/m/125 http://go.tanger.fr/m/125?id=xxx 我已经创建了一个正则表达式来选择链接: /(http(s)?:\/\/go.tanger.fr\/m\/)(\d+)/i 我想更改链接,使其看起来像这样: Tapez ici le message que vous souhaite

我有这样一个文本:

Tapez ici le message que vous souhaitez envoyer à vos clients http://go.tanger.fr/m/125
http://go.tanger.fr/m/125?id=xxx
我已经创建了一个正则表达式来选择链接:

/(http(s)?:\/\/go.tanger.fr\/m\/)(\d+)/i
我想更改链接,使其看起来像这样:

Tapez ici le message que vous souhaitez envoyer à vos clients http://go.tanger.fr/m/125
http://go.tanger.fr/m/125?id=xxx

看起来您只想将最后的数字用作查询字符串

您可以使用

$re = "#(https?):\/\/go\.tanger\.fr\/m\/(\d+)#i"; 
$str = "Tapez ici le message que vous souhaitez envoyer à vos clients https://go.tanger.fr/m/123"; 
$subst = "$1://go.tanger.fr/m/125?id=$2"; 
$result = preg_replace($re, $subst, $str);
以下是使用($string是您要更改的字符串):


这是我服务器上的一个id,我应该添加到我的链接中。你没有混淆输入和输出吗?您的正则表达式用于匹配一些不同的链接类型。因此您有
http://go.tanger.fr/m/125
并想转到
http://go.tanger.fr/m/125?id=xxx
?你想在页面的源代码中这样做还是作为重写/重定向(漂亮的URL)?不,我只想在我的文本中更改它,即all=>如何获取ID?类似这样的内容,我会从
~https?://go\.tanger\.fr/m/(\d+)~I
开始。这对您有用还是需要更多帮助?