G
G
GennadyPHP2018-07-15 14:04:21
PHP
GennadyPHP, 2018-07-15 14:04:21

How can I access the user through regexp?

What regular expression for preg_replace should be written to replace @username with a link in the string? those. there is an example code:

$str_search = ""; // ?
$str_replace = '<a href="/$1">$1</a>';
$text = "Пользователь @username - перейти";
preg_replace($str_search, $str_replace, $text);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-07-15
@GennadyPHP

$str_search = '~\@([^ ]*)~'; 
$str_replace = '<a href="/$1">$1</a>';
$text = "Пользователь @username - перейти";
echo preg_replace($str_search, $str_replace, $text);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question