📜 ⬆️ ⬇️

remove unnecessary HTML tags from user input.

Implementation option



I was thinking here ... To remove all unnecessary (read dangerous) HTML tags without a hemorrhoid without HTML code, it is not necessary to parse it at all in the classical sense of the word, but you can simply convert it to a DOMDocument on the server. Then we will see the wonderful removeChild and removeAttribute medots.

You can do it like this:

public function process($text) {
$document = new DOMDocument('1.0', 'utf-8');
@$document->loadHTML($text);
$this->cleanNode($document->documentElement);
return $this->prepare($document->saveXML());
}

')
I leave the implementation of cleanNode to your discretion, since Russian is good, then German is dead. :)

Source: https://habr.com/ru/post/26201/


All Articles