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

我必须使用 转换字符串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


给定的字符串只是一个演示,顺序可能会改变,因为它是动态的。但结构是一样的。


如何获得所需的输出。我的编码有问题吗。


繁华开满天机
浏览 160回答 2
2回答

墨色风雨

为此,我将preg_replace_callback捕获 中的文本[]、slug 类型(user、career或business)和值(对于 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 text3v4l.org 上的演示

BIG阳

使用@\[([^][]*?)\]而不是@\[(.*?)\]&nbsp;演示和解释。rtrim所有更换后仅执行一次。$Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text';if (preg_match("/@\[([^][]*?)\]\(user:(.*?)\)/", $Rtm, $match)) {&nbsp; &nbsp; $Rtm = preg_replace("/@\[([^][]*?)\]\(user:(.*?)\)/", '<a href="/en/main/profile_page_link/$2">$1</a>, ', $Rtm);}if (preg_match("/@\[([^][]*?)\]\(business:(.*?)\)/", $Rtm, $match)) {&nbsp; &nbsp; $Rtm = preg_replace("/@\[([^][]*?)\]\(business:(.*?)\)/", '<a href="/en/business/$2/about">$1</a>, ', $Rtm);}if (preg_match("/@\[([^][]*?)\]\(career:(.*?)\)/", $Rtm, $match)) {&nbsp; &nbsp; $Rtm = preg_replace("/@\[([^][]*?)\]\(career:(.*?)\)/", '<a href="/en/main/$2/about">$1</a>, ', $Rtm);}$Rtm = rtrim($Rtm, ', ');echo $Rtm;
打开App,查看更多内容
随时随地看视频慕课网APP