HP automatically checks if HTML tags are closed

Source: Internet
Author: User

functionHtmlclose ($body) {    $strlen _var=strlen($body); //does not contain HTML tags    if(Strpos($body, ' < ') = = =false) {        return $body; }    //HTML code Markup    $html _tag= 0; //Digest String    $summary _string= ' '; /** * Arrays are used as HTML tags appearing within the record summary range * Start and end are saved in the left and right names respectively * as String: <H3><P><B>A</B>&L     t;/h3>, Suppose P is not closed * Array is: array (' left ' = = array (' h3 ', ' P ', ' b '), ' right ' = ' b ', ' h3 '); * Full HTML tag only, <? <% and other language markers can produce unpredictable results.*/    $html _array=Array(' Left ' =Array(), ' right ' = =Array());  for($i= 0;$i<$strlen _var; ++$i) {        $current _var=substr($body,$i, 1); if($current _var= = ' < ') {            //HTML code starts            $html _tag= 1; $html _array_str= ' '; } Else if($html _tag= = 1) {            //a section of HTML code ends            if($current _var= = ' > ') {                /** * Remove leading and trailing spaces, such as <br/> < IMG src= ""/> etc. may appear*/                $html _array_str=Trim($html _array_str); /** * To determine if the last character is/, if it is, the label is closed, not recorded*/                if(substr($html _array_str,-1)! = '/') {                      //determines whether the first character/, if it is, is placed in the right unit                    $f=substr($html _array_str, 0, 1); if($f= = '/') {                        //remove/                        $html _array[' Right '] [] =Str_replace(‘/‘, ‘‘,$html _array_str); } Else if($f! = '? ') {                        //To determine if it is a ., if, PHP code, Skip                          /** * To determine if there are half-width spaces, if any, separated by a space, the first cell is an HTML tag * such as <H2 class= "a" > <p C lass= "a" >*/                        if(Strpos($html _array_str, ')!==false) {                            //split into 2 cells, possibly with multiple spaces, such as: 

"" > $html _array[' left '] [] =Strtolower( current(Explode(‘ ‘,$html _array_str, 2))); } Else { /** * * If there are no spaces, the entire string is HTML tags, such as:<b> <p> etc * Uniform conversion to lowercase */ $html _array[' left '] [] =Strtolower($html _array_str); } } } //string Reset $html _array_str= ' '; $html _tag= 0; } Else { /** * The characters between < > are composed of a string * for extracting HTML tags*/ $html _array_str.=$current _var; } } Else { //non-html code only counts--$size; } $ord _var_c=Ord($body{$i}); Switch(true) { case(($ord _var_c& 0xE0) = = 0xC0)://2 bytes $summary _string.=substr($body,$i, 2); $i+ = 1; break; case(($ord _var_c& 0xF0) = = 0xE0)://3 bytes $summary _string.=substr($body,$i, 3); $i+ = 2; break; case(($ord _var_c& 0xF8) = = 0xF0)://4 bytes $summary _string.=substr($body,$i, 4); $i+ = 3; break; case(($ord _var_c& 0xFC) = = 0xF8)://5 bytes $summary _string.=substr($body,$i, 5); $i+ = 4; break; case(($ord _var_c& 0xFE) = = 0xFC)://6 bytes $summary _string.=substr($body,$i, 6); $i+ = 5; break; default://1 bytes $summary _string.=$current _var; } } if($html _array[' left ']) { /** compared to the left and right HTML tags, not enough to complete*/ /** * Swap left order, The order of replenishment should be opposite to the order in which HTML appears * if the string to be complete is: <H2>ABC<B>ABC<P>ABC * The order of replenishment should be :</p></b>*/ $html _array[' Left '] =Array_reverse($html _array[' left ']); foreach($html _array[' left '] as $index=$tag) { //determine if the label appears in right $key=Array_search($tag,$html _array[' Right ']); if($key!==false) { //appears, Remove the cell from right unset($html _array[' Right '] [$key]); } Else { //does not appear, need to complete $summary _string. = ' </'.$tag. ' > '; } } } return $summary _string;}

Page truncation string garbled:

functionSub$str,$start,$len) {//$str refers to a string, $start The starting position of the string, $len refers to the string length    $len=$len*; $strlen=$start+$len;//storing the total length of a string with $strlen    $tmpstr= ' ';  for($i= 0;$i<$strlen;$i++) {        if(Ord(substr($str,$i, 1)) >0xa0) {//If a character appears in the string, the ASC code is greater than 0xa0. Making judgments is not the same as English Characters.             $tmpstr. =MB_SUBSTR ($str,$i, 2, ' Utf-8 '); //$tmpstr. =substr ($str, $i, 2);            $i++; }        Else            $tmpstr. =MB_SUBSTR ($str,$i, 1, ' Utf-8 '); }    return $tmpstr;}

HP automatically checks if HTML tags are closed

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.