Page 1 of 1

Smilies

Posted: Sat Sep 03, 2011 5:35 am
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?

Re: Smilies

Posted: Sat Sep 03, 2011 6:40 pm
by Christopher
The simplest way might be to only replace smilies that have whitespace characters around them, or at least no alpha-numeric characters.

Re: Smilies

Posted: Sun Sep 04, 2011 5:02 am
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?

Re: Smilies

Posted: Sun Sep 04, 2011 5:12 pm
by Christopher
Add a period to the replacement string '.<img src="smile.png" />'

Re: Smilies

Posted: Mon Sep 05, 2011 6:43 am
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);

Re: Smilies

Posted: Wed Sep 07, 2011 4:39 pm
by lenton
BUMP

Re: Smilies

Posted: Thu Sep 29, 2011 8:45 am
by grouponscript
Your problem will be solved the help of html eneities...

Re: Smilies

Posted: Tue Dec 20, 2011 1:24 pm
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!

Re: Smilies

Posted: Tue Dec 20, 2011 2:04 pm
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?