note 83741 deleted from function.strip-tags by danbrown

From: Date: Sun, 17 May 2009 15:53:15 +0000
Subject: note 83741 deleted from function.strip-tags by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154706@lists.php.net to get a copy of this message
Note Submitter: razonklnbd at hotmail dot com ---- When I attempt to use strip_tags it didn't strip text of that string. But I need to strip text all the text into an html page header code. This function will perform it operation like following way... 1. Check if string contain "<body>" tag 2. If found then keep the body text and remove other staff like css, js or any 3. Then do strip_tag function Its a small but handy function... so I like to share. Function Definition: <?php function extractBodyText($p_str, $p_allowedtag=NULL){ $fstr=(preg_match('/<body[^>]*>(.*?)<\/body>/si', $p_str, $regs)?$fstr=$regs[1]:$p_str); $rtrn=(isset($p_allowedtag)?strip_tags($fstr, $p_allowedtag):strip_tags($fstr)); return $rtrn; } ?> Example: <?php $str01=' <dd> <p class="para"> You can use the optional second parameter to specify tags which should not be stripped. </p> <blockquote><p><b class="note">Note</b>: HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with <i><tt class="parameter">allowable_tags</tt></i>. <br /> </p></blockquote> </dd> '; $str='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> '.$str01.' </body> </html> '; echo('<div>'.extractBodyText($str, '<p>').'</div>'); echo('<div>'.extractBodyText($str01).'</div>'); echo('<div>'.strip_tags($str, '<p>').'</div>'); ?> Result: <div> <p class="para"> You can use the optional second parameter to specify tags which should not be stripped. </p> <p>Note: HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowable_tags. </p> </div><div> You can use the optional second parameter to specify tags which should not be stripped. Note: HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowable_tags. </div><div> Untitled Document <p class="para"> You can use the optional second parameter to specify tags which should not be stripped. </p> <p>Note: HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowable_tags. </p> </div>

« previous php.notes (#154706) next »