为了账号安全,请及时绑定邮箱和手机立即绑定

如何使 php 正则表达式 preg_replace 工作

如何使 php 正则表达式 preg_replace 工作

PHP
繁华开满天机 2023-06-24 17:55:28
我必须使用 转换字符串preg_replace。下面是我需要转换的字符串@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text我已经创建了代码来替换内容,但它不起作用。请检查下面的代码,$Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text';if (preg_match("/@\[(.*?)\]\(user:(.*?)\)/", $Rtm, $match)) {    $Rtm0 = preg_replace("/@\[(.*?)\]\(user:(.*?)\)/", '<a href="/en/main/profile_page_link/$2">$1</a>, ', $Rtm);    $Rtm = rtrim($Rtm0, ', ');}if (preg_match("/@\[(.*?)\]\(business:(.*?)\)/", $Rtm, $match)) {    $slug = "1";    $Rtm01 = preg_replace("/@\[(.*?)\]\(business:(.*?)\)/", '<a href="/en/business/' . $slug . '/about">$1</a>, ', $Rtm);    $Rtm = rtrim($Rtm01, ', ');}if (preg_match("/@\[(.*?)\]\(career:(.*?)\)/", $Rtm, $match)) {    $slug = "2";    $Rtm02 = preg_replace("/@\[(.*?)\]\(career:(.*?)\)/", '<a href="/en/main/' . $slug . '/about">$1</a>, ', $Rtm);    $Rtm = rtrim($Rtm02, ', ');}echo $Rtm;上述代码的输出是,<a href="/en/main/profile_page_link/1"> Test Career 12](career:235)</a><a href="/en/business/1/about"> Testing11</a>, @[ Username, some text但我需要的输出是,<a href="/en/main/2/about"> Test Career 12</a>, <a href="/en/business/1/about"> Testing11</a>, <a href="/en/main/profile_page_link/1"> Username</a> some text给定的字符串只是一个演示,顺序可能会改变,因为它是动态的。但结构是一样的。如何获得所需的输出。我的编码有问题吗。
查看完整描述

2 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

为此,我将preg_replace_callback捕获 中的文本[]、slug 类型(usercareerbusiness)和值(对于 slug user)分组,并将它们传递到回调以形成 URL:

$Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text';


$Rtm = preg_replace_callback('/@\[([^]]*)\]\(([a-z]+):([^)]*)\)/', function ($match) {

    switch($match[2]) {

        case 'user':

            return "<a href=\"/en/main/profile_page_link/$match[3]\">$match[1]</a>";

            break;

        case 'business':

            return "<a href=\"/en/business/1/about\">$match[1]</a>";

            break;

        case 'career':

            return "<a href=\"/en/main/2/about\">$match[1]</a>";

            break;

        default:

            return "";

            break;

    }

}, $Rtm);

echo $Rtm;

输出(对于您的输入字符串):


<a href="/en/main/2/about"> Test Career 12</a><a href="/en/business/1/about"> Testing11</a><a href="/en/main/profile_page_link/1"> Username</a>some text

3v4l.org 上的演示


查看完整回答
反对 回复 2023-06-24
?
BIG阳

TA贡献1859条经验 获得超6个赞

使用@\[([^][]*?)\]而不是@\[(.*?)\] 演示和解释

rtrim所有更换后仅执行一次。

$Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text';


if (preg_match("/@\[([^][]*?)\]\(user:(.*?)\)/", $Rtm, $match)) {

    $Rtm = preg_replace("/@\[([^][]*?)\]\(user:(.*?)\)/", '<a href="/en/main/profile_page_link/$2">$1</a>, ', $Rtm);

}


if (preg_match("/@\[([^][]*?)\]\(business:(.*?)\)/", $Rtm, $match)) {

    $Rtm = preg_replace("/@\[([^][]*?)\]\(business:(.*?)\)/", '<a href="/en/business/$2/about">$1</a>, ', $Rtm);

}


if (preg_match("/@\[([^][]*?)\]\(career:(.*?)\)/", $Rtm, $match)) {

    $Rtm = preg_replace("/@\[([^][]*?)\]\(career:(.*?)\)/", '<a href="/en/main/$2/about">$1</a>, ', $Rtm);

}

$Rtm = rtrim($Rtm, ', ');


echo $Rtm;


查看完整回答
反对 回复 2023-06-24
  • 2 回答
  • 0 关注
  • 159 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信