note 91113 deleted from function.strip-tags by thiago
| From: | thiago@php.net | Date: | Tue, 26 May 2009 01:49:58 +0000 |
| Subject: | note 91113 deleted from function.strip-tags by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156008@lists.php.net to get a copy of this message | ||
Note Submitter: brovka75 at mail dot ru
----
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;
}
}
?>