Two ways to complete and filter HTML tags for web content

Source: Internet
Author: User
Tags html form

There are two ways to complete and filter HTML tags for Web content:

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. = ' </'. $opened tags[$i]. ' > ';       } else {         unset ($closedtags [Array_search ($openedtags [$i], $closedtags)]);       }  }  return $html;}

closetags () parsing:
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);p Reg_match_all ("/\< ([^\<]+) \>/is", $html, $ms); $ searchs[] = ' < '; $replaces [] = ' < '; $searchs [] = ' > '; $replaces [] = ' > '; if ($ms [1]) {$allowtags = ' img|font|div|t Able|tbody|tr|td|th|br|p|b|strong|i|u|em|span|ol|ul|li ';//Allowed 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) analysis:

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


Thank you for your attention Websites Blog!

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.