【问题标题】:How to select and change URL into a text using preg_replace如何使用 preg_replace 选择 URL 并将其更改为文本
【发布时间】:2015-06-08 15:15:40
【问题描述】:

我有这样的文字:

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

我已经创建了一个正则表达式来选择链接:

/(http(s)?:\/\/go.tanger.fr\/m\/)(\d+)/i

我想更改链接,使其看起来像这样:

http://go.tanger.fr/m/125?id=xxx

【问题讨论】:

  • 这是我服务器上的一个 ID,我应该添加到我的链接中
  • 你不是把输入输出搞混了吗?您的正则表达式用于匹配一些不同的链接类型。
  • 所以你有http://go.tanger.fr/m/125 并想去http://go.tanger.fr/m/125?id=xxx?你想在页面的源代码中这样做还是作为重写/重定向(漂亮的 URL)?
  • 不,我只是想在我的文字中更改所有go.tanger.fr/m/125 => go.tanger.fr/m/125?id=xxx
  • 如何获得 ID?像这样的东西我会从~https?://go\.tanger\.fr/m/(\d+)~i开始。

标签: php regex


【解决方案1】:

您似乎只想使用最后一位作为查询字符串。

你可以通过

$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);

这里是an IDEONE demo

【讨论】:

  • 它对您有用还是需要更多帮助?
【解决方案2】:

使用($string 是要更改的字符串):

preg_replace('/(http(s)?:\/\/dominos.mylittlebiz.fr\/m\/)(\d+)/i', 'http://go.tanger.fr/m/\1?ID=XXX', $string);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2020-08-20
    • 2023-01-09
    • 2021-04-22
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多