ホームページ  >  記事  >  バックエンド開発  >  PHP は、HTML タグが終了関数コードを自動的に完成させます。

PHP は、HTML タグが終了関数コードを自動的に完成させます。

高洛峰
高洛峰オリジナル
2016-12-01 14:58:061472ブラウズ

いくつかのコードの簡単な説明:
最初の ~(<[^>]+?>)~si
この正規表現は <…………> の内容と一致します。簡単に言うと、すべての です。
2 番目の ~<([a-z0-9]+)[^/>]*?/>~si
この正規表現は <……/> の内容と一致します。
のような単一の閉じたタグです
3 番目の ~]*?>~si
この正規表現は < に一致します。 /.....>。つまり、 のような終了タグ
4 番目の ~<([a-z0-9]+)[^/>]*?>~si
は <......> ; の内容。これは最初のタグとは異なります。HTML タグには次のような数字と文字しか含まれていないためです。
5 番目のタグ~~si
説明の必要はありません。HTMl コメントです
残りはソース コード、php ソース コードの添付ファイルで確認できます:
コードをコピーします コードは次のとおりです:
/**
* HTML を取得し、自動的に完成して閉じます
*
* param $html
*
* param $length
*
* param $end
*/
function subHtml($html, $length=50) {
$tagStack = array();
$contents = preg_split("~(< >]+? >)~si", $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach($contents as $tag) {
if (trim($tag) == "") 続行;
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) $result .= $tag;
$len += mstrlen($tag);
$str = msubstr($tag, $length - $len + 1); $str;
break ;
}
} while (!empty($tagStack)) {
$result .= '>' . $result ;
}
/ **
* 中国語文字列の取得
*
* param $string 文字列
*
* param $start 開始ビット
*
* param $length 長さ
*
* param $charset エンコーディング
*
* param $dot 追加の単語文字列
*/
function msubstr($string, $start, $length, $dot = '', $charset = 'UTF-8') {
$string = str_replace(' &', '" ', '<', '>', ' ')、array('&', '"', '<', '>', ' '), $string); if (strlen($ string) return $string;
}
if (strto lower($charset) == 'utf-8') {
$n = $tn = $noc = 0;
while ($n < strlen($string)) {
$t = ord($string[$n]);
if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1;
$n++;
} elseif (194 <= $t && $t <= 223) {
$n = 2; += 2;
} elseif (224 <= $t && $t <= 239) {
$n = 3;
} elseif (240 <= $t && $t <= 239) ;= 247) {
$tn = 4;
$n += 4;
} elseif (248 <= $t && $t <= 251) {
$n = 5;
} elseif ($ t == 252 || $t == 253) {
$n += 6;
} else {
$n++;
$noc++; >= $length ) {
break;
}
}
if ($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i $strcut .= ord($string[$i]) > $string[$i] . [++$i ] : $string[$i];
}
}
return $strcut . $dot;
/**
* 中国語と英語を含む文字列の長さを取得します。
*/
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]); 
}
$length を返す; 
}
$str = "

x1s
测试124"; 
echo subHtml($str); 
?> 

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。