Smilies

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Smilies

Post by lenton »

I have a preg_replace that replaces ascii faces such as ":)" with an image.

But I have a problem because faces are appearing where you don't want them like in links:
http :? /www.google.com

I want the smiley to only display if it is NOT next to a letter or number.

What would the regex be?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Smilies

Post by Christopher »

The simplest way might be to only replace smilies that have whitespace characters around them, or at least no alpha-numeric characters.
(#10850)
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Smilies

Post by lenton »

Answer this question and I think I will be able to do it:

Is there a way to check if ":)" is next to a full stop but not actually replace it?

Example:

Code: Select all

$post = '.:)';
$post = preg_replace('#\.:\)#', '<img src="smile.png" />', $post);
That replaces the full stop but I need to keep the full stop, how can I do this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Smilies

Post by Christopher »

Add a period to the replacement string '.<img src="smile.png" />'
(#10850)
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Smilies

Post by lenton »

I need to do it in the pattern because I'm going to use \W which isn't always a full stop.

Code: Select all

$post = '@:)';
$post = preg_replace('#\W:\)\W#', '<img src="smile.png" />', $post);
User avatar
lenton
Forum Commoner
Posts: 49
Joined: Sun Jun 20, 2010 6:45 am

Re: Smilies

Post by lenton »

BUMP
grouponscript
Forum Newbie
Posts: 1
Joined: Thu Sep 29, 2011 8:16 am

Re: Smilies

Post by grouponscript »

Your problem will be solved the help of html eneities...
Jacki
Forum Newbie
Posts: 1
Joined: Tue Dec 20, 2011 1:20 pm

Re: Smilies

Post by Jacki »

I have been struggling with the same issue. I am pretty close to giving up on this all together if I can't work it out soon. I have tried the suggestions given above, but yet I still end up with smilies where they do not belong. Are there any other suggestions? I would really like to figure this out and move on asap. I was wondering what you guys do for online file storage? I have a lot of information that I would like to back up in a secure location. If I were ever to lose any of this information, it would not be a good thing. Thank you for your help!
Last edited by Jacki on Tue Jan 03, 2012 3:19 pm, edited 1 time in total.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: Smilies

Post by ragax »

Hi Lenton,

It sounds like you want to test if what surrounds your smilies to make sure the replacement is legitimate. You need to use "lookarounds", which include "lookbehinds" and "lookaheads".

Let's take your example with http: //google.com (space inserted to avoid conversion).

Here's an example of lookahead to solve the problem:

Code: Select all

(?!://):/
That means match a colon followed by a slash, only if what is ahead is not the sequence of letters colon slash slash.

Here's an example of lookbehind to solve it:

Code: Select all

(?<!http):/
That means match a colon followed by a slash, only if what is behind is not http.

You could combine the two, either straight up, or with alternations (|)

With that syntax, you should be able to address all of your smiley situations.

Is that a step in the right direction?
Post Reply