PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Dec 08, 2019 8:37 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Smilies
PostPosted: Sat Sep 03, 2011 5:35 am 
Offline
Forum Commoner
User avatar

Joined: Sun Jun 20, 2010 6:45 am
Posts: 49
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?


Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Sat Sep 03, 2011 6:40 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
The simplest way might be to only replace smilies that have whitespace characters around them, or at least no alpha-numeric characters.

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Sun Sep 04, 2011 5:02 am 
Offline
Forum Commoner
User avatar

Joined: Sun Jun 20, 2010 6:45 am
Posts: 49
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:
Syntax: [ Download ] [ Hide ]
$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?


Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Sun Sep 04, 2011 5:12 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
Add a period to the replacement string '.<img src="smile.png" />'

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Mon Sep 05, 2011 6:43 am 
Offline
Forum Commoner
User avatar

Joined: Sun Jun 20, 2010 6:45 am
Posts: 49
I need to do it in the pattern because I'm going to use \W which isn't always a full stop.

Syntax: [ Download ] [ Hide ]
$post = '@:)';
$post = preg_replace('#\W:\)\W#', '<img src="smile.png" />', $post);


Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Wed Sep 07, 2011 4:39 pm 
Offline
Forum Commoner
User avatar

Joined: Sun Jun 20, 2010 6:45 am
Posts: 49
BUMP


Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Thu Sep 29, 2011 8:45 am 
Offline
Forum Newbie

Joined: Thu Sep 29, 2011 8:16 am
Posts: 1
Your problem will be solved the help of html eneities...


Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Tue Dec 20, 2011 2:24 pm 
Offline
Forum Newbie

Joined: Tue Dec 20, 2011 2:20 pm
Posts: 1
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 ? 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 4:19 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Smilies
PostPosted: Tue Dec 20, 2011 3:04 pm 
Offline
Forum Commoner
User avatar

Joined: Thu Dec 15, 2011 2:40 pm
Posts: 85
Location: Nelson, NZ
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:

Syntax: [ Download ] [ Hide ]
(?!://):/


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:

Syntax: [ Download ] [ Hide ]
(?<!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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group