Smilies displayed in forums

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Smilies displayed in forums

Post by johnny_control »

Quite an easy subject really, but with a slight added complexity.

Imagine someone posting this in the forums:

Code: Select all

Some default text:
 
<code>public function foo($bar = "foobar") { ... }</code>
Everything between <code> tags is turned to their entity so that they are not evaluated. So it turns into:

Code: Select all

Some default text:
 
<code>public function foo($bar = &quot;foobar&quot;) { ... }</code>
Now, when this is then parsed for smileys it finds a smiley winking ;) at the end of the function call. But since it is only displaying code it should not appear in there. The dilemma is thus:

If smileys are parsed before the code tags are transformed then end up with img tags in the code and if you you run the smiley code after then you end up with smileys in the actual code tags.

Anyone have any good ideas to get around this?
Thanks!
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Smilies displayed in forums

Post by johnny_control »

I'm still stuck with this and flat out of ideas. Anyone help a man in distress?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Smilies displayed in forums

Post by novice4eva »

I was thinking like since the code is already inside <code></code>, it should not convert anything within those to smileys!! Whatever you are doing to get the smileys, i think it could be possible to modify the code to avoid anything between <code> </code>
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Smilies displayed in forums

Post by johnny_control »

novice4eva wrote:I was thinking like since the code is already inside <code></code>, it should not convert anything within those to smileys!! Whatever you are doing to get the smileys, i think it could be possible to modify the code to avoid anything between <code> </code>
Well, yea. But does anyone have any ideas on how to do that?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Smilies displayed in forums

Post by novice4eva »

when converting things into their entites using ENT_NOQUOTES attribute. You must have done htmleltities($var) right?? just in its place put htmleltities($var,ENT_NOQUOTES) and tell us what you get. If it doesn't work out, send us the code that you are using for parsing, maybe we could work something out
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Smilies displayed in forums

Post by johnny_control »

Code: Select all

private function parseCode($code) {
    $code = htmlentities($code[1], ENT_QUOTES, 'UTF-8');
    return '<code>'.str_replace(array("[", "]", ":", "(", ")", "|", " "), array("&#091;", "&#093;", "&#058;", "&#040;", "&#041;", "&#124;", "&nbsp;"), $code).'</code>';
}
That is the function called after a preg_replace_callback regex of:

Code: Select all

#\<code\>(.*)?\<\/code\>#isU
Thanks!
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Smilies displayed in forums

Post by novice4eva »

as i said in my earlier post lets try this

Code: Select all

 
htmlentities($code[1], ENT_NOQUOTES, 'UTF-8');
 
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Smilies displayed in forums

Post by johnny_control »

novice4eva wrote:as i said in my earlier post lets try this

Code: Select all

 
htmlentities($code[1], ENT_NOQUOTES, 'UTF-8');
 
That wouldn't work, though. That would just not convert " and '. You still have the < (<) and > (>) that will cause problems.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Smilies displayed in forums

Post by novice4eva »

i ran your code like this

Code: Select all

 
    $code = '<code>public function foo($bar = "foobar"){}</code>';
    function parseCode($code) {
       $code = htmlentities($code[0], ENT_QUOTES, 'UTF-8');
       return '<code>'.str_replace(array("[", "]", ":", "(", ")", "|", " "), array("&#091;", "&#093;", "&#058;", "&#040;", "&#041;", "&#124;", "&nbsp;"), $code).'</code>';
    }
    $regex = '#\(.*)?\#';
    echo preg_replace_callback($regex,'parseCode',$code);
 
And the equivalent html source code came out like this

Code: Select all

<code><code>public&nbsp;function&nbsp;foo&#040;$bar&nbsp;=&nbsp;"foobar"&#041;{}</code></code>
this shouldn't get us smileys ..sud it??
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Smilies displayed in forums

Post by johnny_control »

novice4eva wrote:this shouldn't get us smileys ..sud it??
Umm. Yes. But then consider someone posting this:

Code: Select all

<code>H1 tag is for headings (<h1>...</h1>)</code>
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Smilies displayed in forums

Post by Syntac »

HTML isn't evaluated in <code> tags anyway (right?), but then you'll just end up with a big ol' <img src="path/to/smiley.png" /> smack dab in the middle of your function definition.
johnny_control
Forum Newbie
Posts: 16
Joined: Sat Dec 13, 2008 5:42 am

Re: Smilies displayed in forums

Post by johnny_control »

Syntac wrote:HTML isn't evaluated in <code> tags anyway (right?), but then you'll just end up with a big ol' <img src="path/to/smiley.png" /> smack dab in the middle of your function definition.
Exactly. Which isn't what the site wants. If someone puts <code>:)</code> it should display as that, not <code><img src="..." /></code>

Sorry for late reply, but was busy over Christmas.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Smilies displayed in forums

Post by novice4eva »

johnny_control wrote:
novice4eva wrote:this shouldn't get us smileys ..sud it??
Umm. Yes. But then consider someone posting this:

Code: Select all

<code>H1 tag is for headings (<h1>...</h1>)</code>
Still i don't think this should also get us any smiley :D , the output was

Code: Select all

<code><code>H1&nbsp;tag&nbsp;is&nbsp;for&nbsp;headings&nbsp;&#040;<h1>...</h1>&#041;</code></code>
Can you send the function that does the smiley conversion thing, need to test for sure what output you are getting.
Post Reply