A
A
Archakov Dennis2016-01-31 17:16:23
PHP
Archakov Dennis, 2016-01-31 17:16:23

How to replace all tags with RegExp in PHP?

I implemented comments on my project and when quoting, I decided to enter the following tag as a name: [@UserName] , which in turn will be a link when displayed on the site.
How to replace all such tags with links in the entire text.
Example: " [@Vasya Pupkin], hello!! Do you know this dude [@Vladimir Vladimirovich]??? "
Will be displayed as:

<a href="/user/Вася Пупкин">@Вася Пупкин</a>, привет!! Ты знаешь этого чувака <a href="/user/Вася Пупкин">@Владимир Владимирович</a>???

My crooked code only replaces @ :
echo preg_replace("/#(\w*[a-zA-Z_0-9]+\w*)/", "\1", $text);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wol_fi, 2016-01-31
@archakov06

preg_replace('/\[(@([^\]]+))\]/', '<a href="/user/$2">$1</a>', $text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question