private function filterTags($source)
{
$source = preg_replace('/\\<script([^\\>]*)\\>(.*)\\<\\/script\\>/isU', '', $source);
if (in_array('iframe', $this->disabletags)) {
if (preg_match_all("/<iframe[a-z0-9\\s\\=\"]*src\\=\"(http(s)?\\:)?\\/\\/([w]{3})?\\.youtube[^\\/]+\\/embed\\/([^\\?]+)(\\?[^\"]+)?\"[^\\>]*\\><\\/iframe>/isU", $source, $match)) {
foreach ($match[0] as $key => $_m) {
$vid = $match[4][$key];
$width = intval(preg_replace("/^(.*)width\\=\"([\\d]+)\"(.*)\$/isU", "\\2", $_m));
$height = intval(preg_replace("/^(.*)height\\=\"([\\d]+)\"(.*)\$/isU", "\\2", $_m));
$width = $width > 0 ? $width : 480;
$height = $height > 0 ? $height : 360;
$ojwplayer = '<object height="' . $height . '" width="' . $width . '"><param name="movie" value="//www.youtube.com/v/' . $vid . '?rel=0&hl=pt_BR&version=3" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="' . $height . '" src="//www.youtube.com/v/' . $vid . '?rel=0&autoplay=1&hl=pt_BR&version=3" type="application/x-shockwave-flash" width="' . $width . '"></embed></object>';
$source = str_replace($_m, $ojwplayer, $source);
}
}
}
$preTag = null;
$postTag = $source;
$tagOpen_start = strpos($source, '<');
while ($tagOpen_start !== false) {
$preTag .= substr($postTag, 0, $tagOpen_start);
$postTag = substr($postTag, $tagOpen_start);
$fromTagOpen = substr($postTag, 1);
$tagOpen_end = strpos($fromTagOpen, '>');
if ($tagOpen_end === false) {
break;
}
$tagOpen_nested = strpos($fromTagOpen, '<');
if ($tagOpen_nested !== false && $tagOpen_nested < $tagOpen_end) {
$preTag .= substr($postTag, 0, $tagOpen_nested + 1);
$postTag = substr($postTag, $tagOpen_nested + 1);
$tagOpen_start = strpos($postTag, '<');
continue;
}
$tagOpen_nested = strpos($fromTagOpen, '<') + $tagOpen_start + 1;
$currentTag = substr($fromTagOpen, 0, $tagOpen_end);
$tagLength = strlen($currentTag);
if (!$tagOpen_end) {
$preTag .= $postTag;
$tagOpen_start = strpos($postTag, '<');
}
$tagLeft = $currentTag;
$attrSet = array();
$currentSpace = strpos($tagLeft, ' ');
if (substr($currentTag, 0, 1) == '/') {
$isCloseTag = true;
list($tagName) = explode(' ', $currentTag);
$tagName = strtolower(substr($tagName, 1));
} else {
$isCloseTag = false;
list($tagName) = explode(' ', $currentTag);
$tagName = strtolower($tagName);
}
if (!preg_match('/^[a-z][a-z0-9]*$/i', $tagName) || in_array($tagName, $this->disabletags)) {
$postTag = substr($postTag, $tagLength + 2);
$tagOpen_start = strpos($postTag, '<');
continue;
}
while ($currentSpace !== false) {
$fromSpace = substr($tagLeft, $currentSpace + 1);
$nextSpace = strpos($fromSpace, ' ');
$openQuotes = strpos($fromSpace, '"');
$closeQuotes = strpos(substr($fromSpace, $openQuotes + 1), '"') + $openQuotes + 1;
if (strpos($fromSpace, '=') !== false) {
if ($openQuotes !== false && strpos(substr($fromSpace, $openQuotes + 1), '"') !== false) {
$attr = substr($fromSpace, 0, $closeQuotes + 1);
} else {
$attr = substr($fromSpace, 0, $nextSpace);
}
} else {
$attr = substr($fromSpace, 0, $nextSpace);
}
if (!$attr) {
$attr = $fromSpace;
}
$attrSet[] = $attr;
$tagLeft = substr($fromSpace, strlen($attr));
$currentSpace = strpos($tagLeft, ' ');
}
if (!$isCloseTag) {
$preTag .= '{@[' . $tagName;
if (!empty($attrSet)) {
$attrSet = $this->filterAttr($attrSet);
$preTag .= ' ' . implode(' ', $attrSet);
}
$preTag .= strpos($fromTagOpen, '</' . $tagName) ? ']@}' : ' /]@}';
} else {
$preTag .= '{@[/' . $tagName . ']@}';
}
$postTag = substr($postTag, $tagLength + 2);
$tagOpen_start = strpos($postTag, '<');
}
$preTag .= $postTag;
$preTag = str_replace(array("'", '"', '<', '>'), array("'", """, "<", ">"), $preTag);
return trim(str_replace(array("[@{", "}@]", "{@[", "]@}"), array('"', '"', "<", '>'), $preTag));
}