PHP implementation of Web content HTML tag completion and filtering methods

Source: Internet
Author: User

This article mainly introduces the PHP implementation of Web content HTML tag completion and filtering methods, combined with examples of PHP common tag inspection, completion, closure, filtering and other related operating skills, the need for friends can refer to the next

In this paper, we describe how to implement HTML tag completion and filtering for Web page content in PHP. Share to everyone for your reference, as follows:

If the HTML tag of your Web page is incomplete, some of the table tabs are incomplete and the page is cluttered, or if you include a partial HTML page outside your content, we can write a function method to complement the HTML tag and filter out the useless HTML tags.

PHP makes HTML tags auto-complete, closed, filter function method one:

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()Analytical:

array_reverse(): This function flips the order of elements in the original array, creates a new array, and returns. If the second parameter is specified as true, the key name of the element remains unchanged or the key names are lost.

array_search(): Array_search (value,array,strict), this function looks for a key value in the array like In_array (). If the value is found, the key name of the matching element is returned. Returns False if it is not found. If the third parameter, strict, is specified as true, the key name of the corresponding element is returned only if the data type and value are consistent.

PHP makes HTML tags auto-complete, closed, Filter function method two:

function checkhtml ($html) {$html = Stripslashes ($html);    Preg_match_all ("/\< ([^\<]+) \>/is", $html, $ms);    $searchs [] = ' < ';    $replaces [] = ' < ';    $searchs [] = ' > ';    $replaces [] = ' > '; if ($ms [1]) {$allowtags = ' img|font|p|table|tbody|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li ';//allowable label $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;} Cancels the HTML code function shtmlspecialchars ($string) {if(Is_array ($string))    {foreach ($string as $key = + $val) {$string [$key] = Shtmlspecialchars ($val); }} else {$string = Preg_replace ('/& (# (\d{3,5}|x[a-fa-f0-9]{4}) |[ a-za-z][a-z0-9]{2,5});)/', ' &\\1 ', Str_replace (' & ', ' ' ', ' < ', ' > '), Array (' & ', ' "', ' < ', '  > '), $string)); } return $string;}

checkhtml($html)Analytical:

stripslashes(): The function removes addslashes() the backslash added by the function. This function cleans up data retrieved from a database or HTML form.

The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: