Summary of methods for completing and filtering html tags of web content in PHP [2 methods] and completing Web Content
This example describes how PHP can complete and filter html tags on webpage content. We will share this with you for your reference. The details are as follows:
If the html tags of Your webpage are not fully displayed, some table tags are incomplete, leading to page confusion, or some html pages other than your content are included, we can write a function to complete html tags and filter out useless html tags.
Php makes HTML tags automatically complete and closed. method 1 of filtering functions:
Code:
function closetags($html) { preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result); $openedtags = $result[1]; preg_match_all('#</([a-z]+)>#iU', $html, $result); $closedtags = $result[1]; $len_opened = count($openedtags); if (count($closedtags) == $len_opened) { return $html; } $openedtags = array_reverse($openedtags); for ($i=0; $i < $len_opened; $i++) { if (!in_array($openedtags[$i], $closedtags)) { $html .= '</'.$openedtags[$i].'>'; }else { unset($closedtags[array_search($openedtags[$i], $closedtags)]); } } return $html;}
closetags()
Resolution:
array_reverse()
: This function switches the elements in the original array sequentially, creates a new array, and returns it. If the second parameter is set to true, the key name of the element remains unchanged; otherwise, the key name is lost.
array_search()
: Array_search (value, array, strict). This function looks for a key value in the array like in_array. If this value is found, the key name of the matching element is returned. If not found, false is returned. If the third parameter strict is set to true, the key name of the corresponding element is returned only when the data type and value are consistent.
Php makes HTML tags automatically complete and closed. method 2 of the filter function:
Function checkhtml ($ html) {$ html = stripslashes ($ html); preg_match_all ("/\ <([^ \ <] +) \>/is", $ html, $ ms); $ searchs [] = '<'; $ replaces [] = '<'; $ searchs [] = '>'; $ replaces [] = '> '; if ($ ms [1]) {$ allowtags = 'img | font | div | table | tbody | tr | td | th | br | p | B | strong | I | u | em | span | ol | ul | li '; // allowed tags $ ms [1] = array_unique ($ ms [1]); foreach ($ ms [1] as $ value) {$ searchs [] = "<". $ value. ">"; $ value = shtmlspecialchars ($ Value); $ value = str_replace (array ('\', '/*'), array ('. ','/. '), $ value); $ value = preg_replace (array ("/(javascript | script | eval | behaviour | expression)/I ", "/(\ s + |" | ') on/I "), array ('. ','. '), $ value); if (! Preg_match ("/^ [\/| \ s]? ($ Allowtags) (\ s + | $)/is ", $ value) {$ value ='';} $ replaces [] = empty ($ value )? '':" <". Str_replace ('"', '"', $ value ). ">" ;}}$ html = str_replace ($ searchs, $ replaces, $ html); return $ html ;}// cancel the HTML code function shtmlspecialchars ($ string) {if (is_array ($ string) {foreach ($ string as $ key => $ val) {$ string [$ key] = shtmlspecialchars ($ val );}} else {$ string = preg_replace ('/& (# (\ d {} | x [a-fA-F0-9] {4 }) | [a-zA-Z] [a-z0-9] {2, 5});)/',' & \ 1', str_replace (array ('&','"', '<', '>'), array ('&', '"', '<', '>'), $ string);} return $ string ;}
checkhtml($html)
Resolution:
stripslashes()
: Function deletionaddslashes()
The backslash added to the function. This function is used to clear data retrieved from a database or HTML form.