tags:

views:

49

answers:

4

hey guys

i have a textarea with ckeditor customized toolbar which user can only select smiley

now i want users can only use smilys and new line function

so if $comment is my raw text output with html tags and i want only smiley and text with new lines appeared in output not other html tags

sample raw data :

<p>
    this is a sample text</p>
<p>
    <img alt="angry" src="includes/ckeditor/plugins/smiley/images/angry_smile.gif" title="angry" /></p>
<p>
    text for sample</p>

for new line in html :

$comment = nl2br($comment);

but what about showing only smileys ?!

A: 

Not sure exactly what you're trying to do with the smileys, but there is a way to configure CK in the javascript config file to use
when the enter key is pressed. I'd still think about post-process replacing, but that's a good way to make clean code happen on the front end.

bpeterson76
i thought i explained my will compeletly , uh .. just concider im trying to able my users only use smiley and new line in textarea and strip all other html tags
Mac Taylor
Mac, almost seems like it'd be easier (and less overhead) to do a textarea that you code to take smileys....a roll-it-yourself smiley WYSIWYG. See this: http://www.phorum.org/phorum5/read.php?8,48517
bpeterson76
+1  A: 

Look at php strip_tags http://php.net/manual/en/function.strip-tags.php. You could do something like

$comment = strip_tags(nl2br($comment),"<br />, #smiley_tag");
pilotgallo2
Or you could use a regex expression to strip away everything except <br /> and whatever your smiley tags code is.
pilotgallo2
`<br />` shouldn't be accepted by `strip_tags` function. You need just `<br>`.
Eugene
i want to stip all html tags except img tag that is used for showing smileys , read my comment on bpeterson76 answer
Mac Taylor
so would you mind updating your answer with ereg_replace() ?!
Mac Taylor
@Mac `ereg_replace` is deprecated, don't use it. Use the preg_* functions instead.
meagar
+2  A: 

If I understood you correctly, then here is reference, to what you might need: strip_tags

Here is what I came up with:

function smileAndText( $some_text = '' ) {

    if( ! empty( $some_text ) ) {

        $some_text = nl2br( $some_text );

        $some_text = strip_tags( $some_text, '<img><br />' );

    }

    return $some_text;

}
Eugene
thanks that works but only for showing img not new line i think i should replace <p> with html entity ? ---- and the other thing is how to specify img tag to only smiley source ?! i mean only smileys allowed to show not other images
Mac Taylor
`strip_tags` is extremely vulnerable to XSS attacks, and doesn't obviate the need for you to encode your output with `htmlspecialchars` or `htmlentities` *before* you run it through `nl2br`.
meagar
+2  A: 

As I answered in your previous question, encode your output with htmlspecialchars and then use nl2br to convert new lines to <br> tags.

To replace text smilies with graphics, you want a series of regular expressions rather than attempting to explode the string and output it in pieces.

The following should give you some idea:

$comment = htmlspecialchars(comment);
$comment = nl2br($comment);

$smilies = array(
  '/\b:\)\b/' => '<img src="smile.gif" />',   // :)
  '/\b:\(\b/' => '<img src="sad.gif" />',     // :(
  '/\b:p\b/'  => '<img src="tongue.gif" />',  // :p
);

$comment = preg_replace(array_keys($smilies), $smilies, $comment);
meagar
thanks but as far as i replied to others' answers , i want to able users to use smiley icon and eugene is correct , and the only problem now is how to restrict img tag to only images from this address : includes/ckeditor/plugins/smiley/images/
Mac Taylor
@Mac You don't have to if you follow my advice and run your text through `htmlspecialchars` before you do your `nl2br` or smiley conversion.
meagar
but meagar i dont want to replace emotion icon with text emotions , i just need to show icons
Mac Taylor
@Mac Then insert custom tags like `[:smile:]` or `[:frown:]`, run the comment through `htmlspecialchars`, and translate your special tags to `<img>` tags. It is fundamentally unsafe to rely on `strip_tags` to remove some tags but not others.
meagar