Answer the question
In order to leave comments, you need to log in
Regular expressions for generating hashtags in PHP?
Good time of the day. There was a need to create hashtags for the site.
With regular expressions, everything is not always smooth for me, so I turned to the Internet for help and, following the example, made the following expressions for hashtags and external links:
$Post_Text = preg_replace("~(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)~", '<noindex><nofollow><a href="http://site.ru/link=$2" target="_blank">$2</a></nofollow></noindex>$3', $Post_Text);
$Post_Text = preg_replace("/#([\S]+)/", "<a href=\"http://site.ru/posts&search=\\1\">#\\1</a>", $Post_Text);
$Post_Text = preg_replace("/@([\S]+)/", "<a href=\"http://site.ru/\\1\">@\\1</a>", $Post_Text);
#[email protected]
@asfgas#asgasg
@[email protected]
#asfasf#asgas
Answer the question
In order to leave comments, you need to log in
Design [^]
to help you.
$Post_Text = preg_replace("/#([^\b#@]+)/", "<a href='http://site.ru/posts&search=$1'>#$1</a>", $Post_Text);
$Post_Text = preg_replace("/@([^\b#@]+)/", "<a href='http://site.ru/$1'>@$1</a>", $Post_Text);
$Post_Text = preg_replace('/(?:^|\s)#([\S]+)/gim', "<a href=\"http://site.ru/posts&search=\\1\">#\\1</a>", $Post_Text);
$Post_Text = preg_replace('/(?:^|\s)@([\S]+)/gim', "<a href=\"http://site.ru/\\1\">@\\1</a>", $Post_Text);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question