note 91113 added to function.strip-tags
| From: | brovka75 at mail dot ru | Date: | Tue, 26 May 2009 01:40:43 +0000 |
| Subject: | note 91113 added to function.strip-tags | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156007@lists.php.net to get a copy of this message | ||
Thanks Nick for strip_tags_attributes. It's my version this function:
<?php
// reworked from http://ua.php.net/manual/ru/function.strip-tags.php
// (nick at optixsolutions dot co dot uk) strip_tags_attributes
// Usage:
// Allowable attributes can be comma seperated
// or '|' separated (without spaces) or array
// Example:
// $allowedTags = "<b><strong><i><em><u><p><p/>"
// .
"<sup><sub><div><img><span><br><br/><ul><ol><li>"
// .
"<s><strike><tt><big><small><pre><blockquote><nobr><wbr>"
// . "<hr><center><table><tr><th><td>"
// . "<h1><h2><h3><h4><h5>"
// ;
// $allowedAttr = "src|alt|align|width|height|color|size|"
// . "border|colspan|rowspan|noshade|clear";
// $htmlStripper = new HtmlStripper();
// $htmlStripper->setFilter($allowedTags, $allowedAttr, "i");
// print "["
// . $htmlStripper->strip('<a href="localhost/test.jpg">'
// . '<img id="im1" src="localhost"/> test image</a>')
// . "]\n";
// Output:
// [<img src="localhost" /> test image]
class HtmlStripper
{
protected $allowedTags;
protected $allowedAttr;
protected $searchParams = "i";
private $debug = false;
public function __construct()
{
}
public function __destruct()
{
}
public function setFilter
(
$allowedTags = NULL
, $allowedAttr = NULL
, $searchParams = "i"
)
{
$this->allowedTags = $allowedTags;
$this->allowedAttr = $allowedAttr;
$this->searchParams = $searchParams;
}
function attributesCallback($matches)
{
if ($this->debug)
{
print "\nchanger matches2 = '{$matches[2]}'";
}
$rep =
'/(\s+(?:(?!'
. $this->allowedAttr
. ')[^=]+)=(?:\'[^\'<>]*\'|"[^"<>]*"))/'
. $this->searchParams
;
//print "\nrep = {$rep}";
return
"<{$matches[1]}"
. preg_replace($rep, '', $matches[2])
. ($matches[3]? " " : "")
. "{$matches[3]}>"
;
}
function strip($string)
{
$string = strip_tags($string, $this->allowedTags);
if (!$this->allowedTags)
{
return $string;
}
if (!is_array($this->allowedAttr))
{
$this->allowedAttr = strtr($this->allowedAttr, ",", "|");
}
if (is_array($this->allowedAttr))
{
$this->allowedAttr = implode("|", $this->allowedAttr);
}
$rep =
'/<([^>\s]*)((?:\s+[^<>=]+='
.
'(?:\'[^\'<>]*\'|"[^"<>]*"))+)\s*(\/)?\s*>/'
. $this->searchParams
;
$string = preg_replace_callback
(
$rep
, array(&$this, 'attributesCallback')
, $string
);
if ($this->debug)
{
print "\nstripped attrs = '$string'\n";
}
return $string;
}
}
?>
----
Server IP: 213.186.192.247
Probable Submitter: 95.133.155.39
----
Manual Page -- http://www.php.net/manual/en/function.strip-tags.php
Edit -- https://master.php.net/note/edit/91113
Del: integrated -- https://master.php.net/note/delete/91113/integrated
Del: useless -- https://master.php.net/note/delete/91113/useless
Del: bad code -- https://master.php.net/note/delete/91113/bad+code
Del: spam -- https://master.php.net/note/delete/91113/spam
Del: non-english -- https://master.php.net/note/delete/91113/non-english
Del: in docs -- https://master.php.net/note/delete/91113/in+docs
Del: other reasons-- https://master.php.net/note/delete/91113
Reject -- https://master.php.net/note/reject/91113
Search -- https://master.php.net/manage/user-notes.php