PHP intercepts HTML strings and automatically complements HTML tags, and intercepts the complete _php tutorial

Source: Internet
Author: User

PHP intercepts HTML strings and automatically complements HTML tags, capturing complete


This article describes how PHP intercepts HTML strings and automatically complements HTML tags. Share to everyone for your reference. The specific analysis is as follows:

Here summarizes the use of PHP to intercept HTML strings automatically complete HTML tags, the actual development will often encounter, many people directly strip_tags filter out the HTML tags, but only the plain text, the readability is very poor, the following is a function, the code is as follows:
Copy CodeThe code is as follows:/**
* Capture HTML and auto-complete closure
* @param $html
* @param $length
* @param $end
*/
function subhtml ($html, $length) {
$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 ("~ ]*?>~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 (!emptyempty ($tagStack)) {
$result. = ' ';
}
return $result;
}

/**
* Intercept Chinese strings
* @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;
}

/**
* Get 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-xbf][x80-xbf]| [Xf1-xf7] [X80-XBF] [X80-XBF] [x80-xbf]/], $text, $ar);
$length =count ($ar [0]);
}
return $length;
}
example, the code is as follows:
Copy CodeThe code is as follows: * @param the HTML to intercept $STR
* Number of @param intercepted $num
* @param need to add more $more
* @return Intercept string
*/
function phpos_chsubstr_ahtml ($str, $num, $more =false)
{
$leng =strlen ($STR);
if ($num >= $leng)
return $str;
$word = 0;
$i = 0; /** string Pointer **/
$stag =array (Array ()); /** store the start HTML flag **/
$etag =array (Array ()); /** store End HTML flag **/
$SP = 0;
$EP = 0;
while ($word! = $num)
{
if (Ord ($str [$i]) >128)
{
$re. =substr ($str, $i, 3);
$i +=3;
$word + +;
}
else if ($str [$i]== ' < ')
{
if ($str [$i +1] = = '! ')
{
$i + +;
Continue
}

if ($str [$i +1]== '/')
{
$ptag =& $etag;
$k =& $ep;
$i +=2;
}
Else
{
$ptag =& $stag;
$i +=1;
$k =& $sp;
}

for (; $i < $leng; $i + +)
{
if ($str [$i] = = ")
{
$ptag [$k] = Implode (", $ptag [$k]);
$k + +;
Break
}
if ($str [$i]! = ' > ')
{
$ptag [$k][]= $str [$i];
Continue
}
Else
{
$ptag [$k] = Implode (", $ptag [$k]);
$k + +;
Break
}
}
$i + +;
Continue
}
Else
{
$re. =substr ($str, $i, 1);
$word + +;
$i + +;
}
}
foreach ($etag as $val)
{
$key 1=array_search ($val, $stag);
if ($key 1!== false) unset ($stag [$key]);
}
foreach ($stag as $key = $val)
{
if (In_array ($val, Array (' BR ', ' img ')) unset ($stag [$key 1]);
}
Array_reverse ($stag);
$ends = ' ';
$re = substr ($str, 0, $i). $ends;
if ($more) $re. = ' ... ';
return $re;
}
PHP intercepts the string, generate a summary of the article, we often need to write a blog to display the first part of the article, but also afraid of improper truncation to destroy the closed tag to cause the entire document structure damage, the code is as follows:
Copy CodeThe code is as follows: function Text_zhaiyao ($text, $length) {///Article Summary generation function $test: Content $length: summary length
Global $Briefing _length;
Mb_regex_encoding ("UTF-8");
if (Mb_strlen ($text) <= $length) return $text;
$Foremost = mb_substr ($text, 0, $length);
$re = "< (/?)
(p| div| h1| h2| h3| h4| h5| h6| address| pre| table|tr| td|th| Input| Select| textarea| Object| a| ul| ol| li|
base| meta| link|hr|br| param| Img| area| Input| SPAN) [^>]* (;?) ";
$Single = "/base| meta| link|hr|br| param| Img| area| Input|br/i ";

$Stack = Array (); $posStack = Array ();

Mb_ereg_search_init ($Foremost, $re, ' I ');

while ($pos = Mb_ereg_search_pos ()) {
$match = Mb_ereg_search_getregs ();
/* [child-matching formulation]:

$matche [1]: A "/" charactor indicating whether "<...>" friction is
Closing part
$matche [2]: Element Name.
$matche [3]: Right > of a "<...>" friction
*/
if ($match [1]== "") {
$Elem = $match [2];
if (Mb_eregi ($Single, $Elem) && $match [3]! = "") {
Continue
}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/943416.html www.bkjia.com true http://www.bkjia.com/PHPjc/943416.html techarticle PHP intercepts HTML strings and automatically complements the HTML tag method, the interception of complete this article describes the PHP interception of HTML strings and automatic completion of HTML tags method. Share to everyone for your reference ...

  • 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.

    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.