note 83741 added to function.strip-tags
| From: | razonklnbd at hotmail dot com | Date: | Tue, 10 Jun 2008 09:45:04 +0000 |
| Subject: | note 83741 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140292@lists.php.net to get a copy of this message | ||
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:
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:
$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>
----
Server IP: 69.147.83.197
Probable Submitter: 202.59.136.14
----
Manual Page -- http://www.php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/83741
Del: integrated -- https://master.php.net/note/delete/83741/integrated
Del: useless -- https://master.php.net/note/delete/83741/useless
Del: bad code -- https://master.php.net/note/delete/83741/bad+code
Del: spam -- https://master.php.net/note/delete/83741/spam
Del: non-english -- https://master.php.net/note/delete/83741/non-english
Del: in docs -- https://master.php.net/note/delete/83741/in+docs
Del: other reasons-- https://master.php.net/note/delete/83741
Reject -- https://master.php.net/note/reject/83741
Search -- https://master.php.net/manage/user-notes.php