Page 1 of 1

Smilies displayed in forums

Posted: Sat Dec 13, 2008 5:54 am
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!

Re: Smilies displayed in forums

Posted: Wed Dec 17, 2008 9:06 am
by johnny_control
I'm still stuck with this and flat out of ideas. Anyone help a man in distress?

Re: Smilies displayed in forums

Posted: Fri Dec 19, 2008 12:20 pm
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>

Re: Smilies displayed in forums

Posted: Sun Dec 21, 2008 12:35 pm
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?

Re: Smilies displayed in forums

Posted: Sun Dec 21, 2008 10:11 pm
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

Re: Smilies displayed in forums

Posted: Mon Dec 22, 2008 3:29 am
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!

Re: Smilies displayed in forums

Posted: Mon Dec 22, 2008 7:12 am
by novice4eva
as i said in my earlier post lets try this

Code: Select all

 
htmlentities($code[1], ENT_NOQUOTES, 'UTF-8');
 

Re: Smilies displayed in forums

Posted: Mon Dec 22, 2008 10:57 am
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.

Re: Smilies displayed in forums

Posted: Mon Dec 22, 2008 7:59 pm
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??

Re: Smilies displayed in forums

Posted: Tue Dec 23, 2008 4:02 am
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>

Re: Smilies displayed in forums

Posted: Tue Dec 23, 2008 10:23 pm
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.

Re: Smilies displayed in forums

Posted: Mon Dec 29, 2008 6:31 am
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.

Re: Smilies displayed in forums

Posted: Mon Dec 29, 2008 8:59 pm
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.