PHP intercepts HTML strings automatically complement HTML tags

Source: Internet
Author: User
Tags foreach html tags ord
The code is as follows Copy Code

/**
* Interception of HTML, and automatic completion of the full 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) = = "") &nbsp; 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&nbsp; $result;
}

/**
* Intercept Chinese strings
* @param $string string
* @param $start starting position
* @param $length length
* @param $charset &nbsp; Coding
* @param $dot Additional strings
*/
function Msubstr ($string, $start, $length, $dot = ', $charset = ' UTF-8 ') {
$string = Str_replace (' &amp; ', ' &quot; ', ' &lt; ', ' &gt; ', ' &nbsp; '), 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);&nbsp;
$length =count ($ar [0]);
}
return $length;
}

Instance

The code is as follows Copy Code

* @param the HTML $str to intercept
* Number of @param intercepted $num
* Do @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 logo **/
$etag =array (Array ()); /** storage 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 = ' </'. Implode (' ></', $stag). ' > ';
$re = substr ($str, 0, $i). $ends;
if ($more) $re. = ' ... ';
return $re;
}

PHP intercepts the string, generates the article summary
We often need to display the previous part of the article when we write a blog, but we are afraid of improper truncation to break the closed tag to cause the entire document structure to break

The code is as follows Copy Code
function Text_zhaiyao ($text, $length) {//Article Digest generating functions $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| F12 F22 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 current ' <...> ' friction is
Closing part
$matche [2]: Element Name.
$matche [3]: Right > A "<...>" friction
*/
if ($match [1]== "") {
$Elem = $match [2];
if (Mb_eregi ($Single, $Elem) && $match [3]!= "") {
Continue
}

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.