0

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.

4
  • 1
    My suggestion is to test your regex here: regexr.com it shows all the steps, the groups and matchings Commented Jun 22, 2020 at 9:05
  • 1
    Use @\[([^][]*?)\] instead of /@\[(.*?)\] Commented Jun 22, 2020 at 9:08
  • Why do you have , at the end of the replacement and then you remove it with rtrim? Commented Jun 22, 2020 at 9:13
  • @Toto thanks, it is working. Commented Jun 22, 2020 at 9:14

2 Answers 2

1

I would use preg_replace_callback for this, capturing the text in [], slug type (user, career or business) and value (for slug user) in groups and passing them through to the callback to form the URLs:

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

Output (for your input string):

<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

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

1

Use @\[([^][]*?)\] instead of @\[(.*?)\] Demo & explanation.

Do the rtrim only once avfter all the replacements.

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

4 Comments

if I change $slug = $match[2] and $Rtm = '@[ Test Career 12](career:235)@[ Testing11](business:2)@[ Testing12](business:10)@[ Username](user:1)some text';. Then it is not working.
Where does $slug = $match[2] come from?
To get the dynamic content
I think we can use substr_replace($Rtm, ' ', strrpos($Rtm, ', '), 1) to remove last occurance of comma.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.