dom - Removing everything from string outside specified tags (PHP) -
question has been updated exclude regex possible solution.
i'm trying build php function allow me strip outside of specified tags while preserving specified tags , content , not sure how this...
for example:
$string = "lorem ipsum <div><p>some video content</p><object></object></div><p>dolor sit</p> amet <img>" some_function($string, "<div><img>"); returns: "<div><p>some video content</p><object></object></div><img>"
thanks help!
ok, think figured out way based on modified version of explode_tags function posted link above:
function explode_tags($chr, $str) { ($i=0, $j=0; $i < strlen($str); $i++) { if ($str{$i} == $chr) { while ($str{$i+1} == $chr) $i++; $j++; continue; } if ($str{$i} == "<") { if (strlen($res[$j]) > 0) $j++; $s = strpos($str, " ", $i); $b = strpos($str, ">", $i); if($s<$b) $end = $s; else $end = $b; $t = substr($str, $i+1, $end-$i-1); $tend = strpos($str, ">", $i); $tclose = strpos($str, "</".$t, $tend); if($tclose!==false) $pos = strpos($str, ">", $tclose); else $pos = strpos($str, ">", $i); $res[$j] .= substr($str, $i, $pos - $i+1); $i += ($pos - $i); $j++; continue; } if ((($str{$i} == "\n") || ($str{$i} == "\r")) && (strlen($res[$j]) == 0)) continue; $res[$j] .= $str{$i}; } return $res; } function filter_tags($content, $tags) { $content = strip_tags($content, $tags); $tags = substr($tags, 1, -1); $d = strpos($tags, "><"); if($d===false) $tags = array($tags); else $tags = explode("><", $tags); $content = explode_tags("", $content); $result=""; foreach($content $c) { $s = strpos($c, " "); $b = strpos($c, ">"); if($s<$b) $end = $s; else $end = $b; $tag = substr($c, 1, $end-1); if(in_array($tag, $tags)) $result.=$c; } return $result; } filter_tags($content, "<img><div><object><embed><iframe><param><script>");
this seems work far, although have tried on few different pieces of content. i'm not great @ this, if has suggestions please share freely...
thanks of answers!
Comments
Post a Comment