Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP converting plain text to hashtag link

Tags:

regex

php

twitter

I am trying to convert user's posts (text) into hashtag clickable links, using PHP.

From what I found, hashtags should only contain alpha-numeric characters.

$text = 'Testing#one #two #three.test';
$text = preg_replace('/#([0-9a-zA-Z]+)/i', '<a href="/hashtag/$1">#$1</a>', $text);

It places links on all (#one #two #three), but I think the #one should not be converted, because it is next to another alpha-numeric character, how to adjust the reg-ex to fix that ?

The 3rd one is also OK, it matches just #three, which I think is correct.

like image 657
adrianTNT Avatar asked Mar 05 '14 15:03

adrianTNT


3 Answers

You could modify your regex to include a negative lookbehind for a non-whitespace character, like so:

(?<!\S)#([0-9a-zA-Z]+)

Working regex example:

http://regex101.com/r/mR4jZ7

PHP:

$text = preg_replace('/(?<!\S)#([0-9a-zA-Z]+)/', '<a href="/hashtag/$1">#$1</a>', $text);

Edit: And to make the expression compatible with other languages (non-english characters):

(?<!\S)#([0-9\p{L}]+)

Working example:

https://regex101.com/r/Pquem3/1

like image 146
Bryan Elliott Avatar answered Oct 28 '22 06:10

Bryan Elliott


With uni-code, html encoded safe and joined regexp; ~(?<!&)#([\pL\d]+)~u

Here some&#39;s tags like #tag1 #tag2#tag3 etc.

like image 24
K-Gun Avatar answered Oct 28 '22 06:10

K-Gun


Finally I have found the solution like: facebook or others hashtag to url solutions, it may be help you too. This code also works with unicode. I have used some of Bangla Unicode, let me know other Languages work as well, I think it will work on any language.

$str = '#Your Text #Unicode #ফ্রিকেলস বা #তিল মেলানিনের #অতিরিক্ত উৎপাদনের জন‍্য হয় যা #সূর্যালোকে #বাড়ে';
$regex = '/(?<!\S)#([0-9a-zA-Z\p{L}\p{M}]+)/mu';
$text = preg_replace($regex, '<a href="' . BASE . 'search?q=$1">#$1</a>', $str);
echo $text;
like image 36
Monzur Avatar answered Oct 28 '22 06:10

Monzur