Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex on ;) smilie

Tags:

regex

A minor inconvenience my users have found is that if they use a smilie such as >_> at the end of parentheses (kind of like this: >_>) then during processing it is run through htmlspecialchars(), making it >_>) - you can see the problem, I think. The ;) at the end is then replaced by the "Wink" smilie.

Can anyone give me a regex that will replace ;) with the smilie, but only if the ; is not the end of an HTML entity? (I'm sure it would involve a lookbehind but I can't seem to understand how to use them >_>)

Thank you!

like image 387
Niet the Dark Absol Avatar asked Feb 26 '23 16:02

Niet the Dark Absol


2 Answers

Handling smileys like ;) is always a bit tricky - the way I would do it is transform it to the "canonical" :wink: before encoding HTML entities, and then changing only canonical-form :{smileyname}: smileys afterwards.

like image 153
Anon. Avatar answered Mar 07 '23 06:03

Anon.


Like this: (?<!&[a-zA-Z0-9]+);\)

The (?>!...) is a zero-width assertion that will only allow the following construct to match text that isn't preceded by the ....

like image 28
SLaks Avatar answered Mar 07 '23 05:03

SLaks