$string = '<p><a href="http://example.com">Link</a></p>'; // via $_POST['post-content']
$dom = new DOMDocument();
$dom->loadHTML($string);
$allowed_attributes = array('id','href', 'src', 'class', 'style', 'colspan', 'rowspan');
foreach($dom->getElementsByTagName('*') as $node){
for($i = $node->attributes->length -1; $i >= 0; $i--){
$attribute = $node->attributes->item($i);
if(!in_array($attribute->name,$allowed_attributes)) $node->removeAttributeNode($attribute);
}
}
$html = $dom->saveHTML();
Result...
<p><a href="%5C%22http://example.com%5C%22">Link</a></p>
...
I tried html_entity_decode($html), but it doesn't work. I don't understand what is causing this problem. I could use some help.
Link
` – miken32 Jun 28 '17 at 23:14