PHP intercept HTML Article _php tutorial

Source: Internet
Author: User
PHP intercepts HTML code, considering the closure of HTML tags


/* Generate Summary
* @param (String) $body
* Text
* @param (int) $size
* Abstract Length
* @param (int) $format
* Input Format ID
*/
function Blog_summary ($body, $size, $format = NULL) {
$_size = Mb_strlen ($body, "utf-8");

if ($_size <= $size) return $body;

PHP filter in input format
/*
if (!isset ($format) && filter_is_php ($format)) {
return $body;
}
*/

$strlen _var = strlen ($body);

does not contain HTML tags
if (Strpos ($body, ' < ') = = = = False) {
Return Mb_substr ($body, 0, $size);
}

Contains truncation flags, priority
if ($e = Strpos ($body, ' )) {
Return Mb_substr ($body, 0, $e);
}

HTML code Markup
$html _tag = 0;

Digest string
$summary _string = ";

/**
* Arrays are used as HTML tags that appear within the record summary scope
* Start and end are saved in left and right keys respectively
* If the string is:

A

, assuming P is not closed
* Array is: Array (' left ' = = Array (' h3 ', ' P ', ' B '), ' right ' = ' B ', ' H3 ');
* Full HTML tag only, */
$html _array = Array (' Left ' + = Array (), ' right ' = = Array ());
for ($i = 0; $i < $strlen _var; + + $i) {
if (! $size) {
Break
}

$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 the trailing spaces, such as
< img src= ""/> may appear leading and trailing spaces
*/
$html _array_str = Trim ($html _array_str);

/**
* 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

/**
* Determine if there are half-width spaces, if any, separated by a space, the first cell is an HTML tag
* IF


*/
if (Strpos ($html _array_str, ")!== false) {
Split into 2 cells, there may be multiple spaces, such as:


$html _array[' left ' [] = Strtolower (current (Explode ('), $html _array_str, 2));
}else{
/**
* * If there are no spaces, the entire string is an HTML tag, such as:

such as
* Uniform conversion to lowercase
*/
$html _array[' left ' [] = Strtolower ($html _array_str);
}
}
}

String Reset
$html _array_str = ";
$html _tag = 0;
}else{
/**
* Make a string of characters between < >
* 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 supplementation should be the opposite of the order in which HTML appears
* If the string to be complete is:

ABCABC

Abc
* The order of replenishment should be:


*/
$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. = ' ';
}
}
}
return $summary _string;
}

-

http://www.bkjia.com/PHPjc/478039.html www.bkjia.com true http://www.bkjia.com/PHPjc/478039.html techarticle PHP intercepts HTML code, taking into account the closure of HTML tags/* Generate Summary * @param (string) $body * Body * @param (int) $size * Summary length * @param (int) $format * output Into the format ...

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