问题
I have some text with twitter style #hashtags. How would I write a function to parse a body of text that might contain an unlimited number of #hashtags, take the text of the hashtag and replace them all with an <a href="tag/[hashtag text]">[hashtag text]</a>
I've thought a lot about how to do this but I am really bad at writing these sorts of functions with regex.
Example text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus #tristique non elit eu iaculis. Vivamus eget ultricies nisi. Vivamus hendrerit at mauris condimentum scelerisque. Donec nibh mauris, pulvinar et #commodo a, porta et tellus. Duis eget ante gravida, convallis augue id, blandit lectus. Mauris euismod commodo mi ut fringilla. Sed felis magna, rhoncus vitae mattis varius, sagittis a eros. Donec eget porta ipsum. #Mauris sed mauris ante. Suspendisse potenti. Donec a #pretium #augue, eget hendrerit orci. Integer cursus scelerisque consequat.
回答1:
Try using this:
$text = "Vivamus #tristique non elit eu iaculis.";
$text = preg_replace('/(?:^|\s)#(\w+)/', ' <a href="tag/$1">$1</a>', $text);
// $text now: Vivamus <a href="tag/tristique">tristique</a> non elit eu iaculis;
Here it is working: https://3v4l.org/WXqTr (click run).
Regex reference: Space or beginning of string, Non capturing group
Original source: Parsing Twitter with RegExp
回答2:
This will work with UTF-8 encoding texts and will show # (hash) before tags in content
preg_replace('/(\#)([^\s]+)/', ' <a href="tag/$2">#$2</a> ', $content);
回答3:
Try this:
preg_replace('/(\#)([^\s]+)/', '<a href="tag/$2">$2</a>', $your_content_here);
That will turn this: This is a #hashtag
into this: This is a <a href="tag/hashtag">hashtag</a>
,
Assuming that you have $your_content_here = 'This is a #hashtag';
回答4:
It will extract and add links to every tag in the text whether it doesn't have any spaces between tags.
$text = "#test Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus #tristique non elit eu iaculis. Vivamus eget ultricies nisi. Vivamus hendrerit at mauris condimentum scelerisque. Donec nibh mauris, pulvinar et #commodo a, porta et tellus. Duis eget ante gravida, convallis augue id, blandits.Repost#high#fashion#photography#Ishan#portart#photo#Shoot#EishaChopra#luxe#twisty#creative#destination#style#Actor#model#beauty#gorgeous#makeup#and#hair#kamaldeep#fashionista#trending ";
$text = preg_replace('/#(\w+)/', ' <a href="tag/$1">$1</a>', $text);
echo $text;
回答5:
The correct answer is this. For more reasons than one. But most importantly, you need to factor in if your string has URLs, all other answers will break otherwise. See example below:
$text = "#test Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus #tristique non elit eu iaculis. Vivamus eget ultricies nisi. Vivamus hendrerit at mauris condimentum scelerisque. Donec nibh mauris, pulvinar et #commodo a, porta et tellus. Duis eget ante gravida, convallis augue id, blandit lectus. Mauris euismod commodo mi ut fringilla. Sed felis magna, rhoncus vitae mattis varius, sagittis a eros. Donec eget porta ipsum. #Mauris sed mauris ante. Suspendisse potenti. Donec a #pretium #augue, eget hendrerit orci. Integer cursus scelerisque consequat. http://www.example.com/#wdwd/dwdqwdqwdqw#dwqdqwdq";
$text = preg_replace('/(^|[\n\s])#([^\s"\t\n\r<:]*)/is', '$1<a href="http://twitter.com/search?q=%23$2">#$2</a>', $text);
echo $text;
回答6:
I combined Joe's and jraede's solution.
UTF-8 safe and correct hashtag format (no comma etc):
preg_replace('~(\#)([^\s!,. /()"\'?]+)~', '<a href="tag/$2">#$2</a>', $text);
来源:https://stackoverflow.com/questions/17245317/parse-text-for-hashtags-and-replace-with-links-using-php