home forums resources search newsjoinmembers: 6499
Hiveminds Network PHP Flash Java Ruby Windows Linux
 videos
 articles
 blogs
 comments
 downloads
sitemap
Hiveminds | Mon, 2005-05-16 12:36  tags:

Here you will find solutions to problems with stripping HTML from program and user input.

printer-friendly version

Hiveminds's picture
This article brought to you by the Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously
 
Hiveminds posted on: Mon, 2005-05-16 12:41.

PHP 4.3 and above strip out the HTML comment tag and do not make exception for them in the allowable_tags parameter.

Answer:

Create a custom function that uses strip_tags() but removes comment tags before and after strip_tags() is used.

<?php

<?php
function strip_tags_c($string$allowed_tags '')
{
   
$allow_comments = ( strpos($allowed_tags'<!-->') !== false );
   if( 
$allow_comments )
   {
       
$string str_replace(array('<!--''-->'), array('&lt;!--''--&gt;'), $string);
       
$allowed_tags str_replace('<!-->'''$allowed_tags);
   }
   
$string strip_tags$string$allowed_tags );
   if( 
$allow_comments $string str_replace(array('&lt;!--''--&gt;'), array('<!--''-->'), $string);
   return 
$string;
}
$text '<p>Test paragraph.</p><!-- Comment --> &lt;!-- Comment 2 --&gt;Other text</p>';
echo 
'1)'.$text;
echo 
strip_tags_c('2)'.$text);
echo 
"n";
// Allow <p>
echo strip_tags_c('<br>3)'.$text'<p>,<!-->');
?>

 

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.

page top