explode an string and html special character filter in php
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 ?!
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);
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;
}
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");
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.
精彩评论