PHP makes HTML tags auto-complete closed function code
Last Update:2016-12-01
Source: Internet
Author: User
Simply explain some of the code:
The first one ~ (<[^>]+?>) ~si
This regular is a match to the content in <.........>. Simply said to be all < tags >.
A second ~< ([a-z0-9]+) [^/>]*?/>~si
This regular is a match to the content in <....../>. is a single closed label such as <br/>
A third ~</([a-z0-9]+) [^/>]*?>~si
This regular is a match to the content in </......>. End tag like </a>
Fourth ~< ([a-z0-9]+) [^/>]*?>~si
Matches the content in <......>. This is not the same as the first one, which is a real HTML tag, because the HTML tag has only numbers and letters, such as:Fifth ~<!--. *?-->~si
No explanation, the HTML comment
The rest can see the source code, PHP source accessories:
Copy the code code as follows:
<?php
/**
* Take HTML and auto-complete closure
*
* param $html
*
* param $length
*
* param $end
*/
function subhtml ($html, $length =50) {
$result = ";
$tagStack = Array ();
$len = 0;
$contents = Preg_split ("~ (<[^>]+?>) ~si", $html,-1, Preg_split_no_empty | Preg_split_delim_capture);
foreach ($contents as $tag) {
if (Trim ($tag) = = "") continue;
if (Preg_match ("~< ([a-z0-9]+) [^/>]*?/>~si", $tag)) {
$result. = $tag;
} else if (Preg_match ("~</([a-z0-9]+) [^/>]*?>~si", $tag, $match)) {
if ($tagStack [count ($tagStack)-1] = = $match [1]) {
Array_pop ($tagStack);
$result. = $tag;
}
} else if (Preg_match ("~< ([a-z0-9]+) [^/>]*?>~si", $tag, $match)) {
Array_push ($tagStack, $match [1]);
$result. = $tag;
} else if (Preg_match ("~<!--. *?-->~si", $tag)) {
$result. = $tag;
} else {
if ($len + mstrlen ($tag) < $length) {
$result. = $tag;
$len + = Mstrlen ($tag);
} else {
$str = msubstr ($tag, 0, $length-$len + 1);
$result. = $str;
Break
}
}
} while (!empty ($tagStack)) {
$result. = ' </'. Array_pop ($tagStack). ' > ';
}
return $result;
}
/**
* Take Chinese string
*
* param $string string
*
* param $start starting bit
*
* param $length length
*
* param $charset Code
*
* param $dot Additional strings
*/
function Msubstr ($string, $start, $length, $dot = ', $charset = ' UTF-8 ') {
$string = Str_replace (' & ', ' ' ', ' < ', ' > ', '), Array (' & ', ' "', ' < ', ' > ', '), $string);
if (strlen ($string) <= $length) {
return $string;
}
if (Strtolower ($charset) = = ' Utf-8 ') {
$n = $tn = $noc = 0;
while ($n < strlen ($string)) {
$t = Ord ($string [$n]);
if ($t = = 9 | | $t = = 10 | | (<= $t && $t <= 126)) {
$tn = 1;
$n + +;
} elseif (194 <= $t && $t <= 223) {
$tn = 2;
$n + = 2;
} elseif (224 <= $t && $t <= 239) {
$tn = 3;
$n + = 3;
} elseif (<= $t && $t <= 247) {
$tn = 4;
$n + = 4;
} elseif (248 <= $t && $t <= 251) {
$tn = 5;
$n + = 5;
} elseif ($t = = 252 | | $t = = 253) {
$tn = 6;
$n + = 6;
} else {
$n + +;
}
$noc + +;
if ($noc >= $length) {
Break
}
}
if ($noc > $length) {
$n-= $tn;
}
$strcut = substr ($string, 0, $n);
} else {
for ($i = 0; $i < $length; $i + +) {
$strcut. = Ord ($string [$i]) > 127? $string [$i]. $string [+ + $i]: $string [$i];
}
}
Return $strcut. $dot;
}
/**
* The length of the string, including Chinese and English.
*/
function Mstrlen ($str, $charset = ' UTF-8 ') {
if (function_exists (' mb_substr ')) {
$length = Mb_strlen ($str, $charset);
} elseif (Function_exists (' iconv_substr ')) {
$length = Iconv_strlen ($str, $charset);
} else {
Preg_match_all ("/[\x01-\x7f]|[ \xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]| [\xe1-\xef] [\X80-\XBF] [\x80-\xbf]|\xf0[\x90-\xbf][\x80-f][\x80-\xbf]| [\xf1-\xf7] [\X80-\XBF] [\X80-\XBF] [\x80-\xbf]/], $text, $ar);
$length = count ($ar [0]);
}
return $length;
}
$STR = "<div><table>x<tr>1s<td> test <td>124";
echo subhtml ($STR);
?>