How can I use php to strip all/any attributes from a tag, say a paragraph tag?
to >
>
Strip attributes using SimpleXML (Standard in PHP5)
'; // define allowable attributes $allowable_atts = array('href','src','alt'); // strip collector $strip_arr = array(); // load XHTML with SimpleXML $data_sxml = simplexml_load_string(''. $data_str .'', 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOXMLDECL); if ($data_sxml ) { // loop all elements with an attribute foreach ($data_sxml->xpath('descendant::*[@*]') as $tag) { // loop attributes foreach ($tag->attributes() as $name=>$value) { // check for allowable attributes if (!in_array($name, $allowable_atts)) { // set attribute value to empty string $tag->attributes()->$name = ''; // collect attribute patterns to be stripped $strip_arr[$name] = '/ '. $name .'=""/'; } } } } // strip unallowed attributes and root tag $data_str = strip_tags(preg_replace($strip_arr,array(''),$data_sxml->asXML()), $allowable_tags); ?> 0 讨论(0) 查看其它6个回答 发布评论: 提交评论 加载中... 验证码 看不清? 提交回复 热议问题