我必须使用 转换字符串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
给定的字符串只是一个演示,顺序可能会改变,因为它是动态的。但结构是一样的。
如何获得所需的输出。我的编码有问题吗。
墨色风雨
BIG阳