I have to convert a string using preg_replace. Below is the string I need to convert
@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Username](user:1)some text
I have created code to repalce the content, but it is not working. Please check the below code,
$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;
Output for the above code is,
<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
But my required output is,
<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
The given string is just a demo, the order may change as it is dynamic. But the structure is same.
How to get the required output. Is there any problem with my coding.
@\[([^][]*?)\]instead of/@\[(.*?)\],at the end of the replacement and then you remove it withrtrim?