Php intercepts html strings to automatically complete html tags

Source: Internet
Author: User

The article will summarize the use of php to capture html strings and automatically complete html tags. In actual development, it will often happen that many people directly filter out html tags by strip_tags, but only plain text is left, the readability is very poor. The following is a function.

The Code is as follows: Copy code

/**
* Intercept HTML and automatically complete and close
* @ 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;
}

/**
* Truncate a Chinese String
* @ Param $ string
* @ Param $ start bit
* @ Param $ length
* @ Param $ charset & nbsp; Encoding
* @ Param $ dot additional string
*/
Function msubstr ($ string, $ start, $ length, $ dot = '', $ charset = 'utf-8 '){
$ String = str_replace (array ('& 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 | (32 <= $ 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 (240 <=$ 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]) & gt; 127? $ String [$ I]. $ string [++ $ I]: $ string [$ I];
}
}

Return $ strcut. $ dot;
}

/**
* Obtain the length of a string, including Chinese and English characters.
*/
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: HTML $ str to be intercepted
* @ Param: Number of interceptions $ num
* @ Param do you need to add more $ more
* @ Return refers to the truncation 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 ();/** mark for storing the start HTML **/
$ Etag = array ();/** mark for storing ending HTML **/
$ 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)
{
$ Key1 = array_search ($ val, $ stag );
If ($ key1! = False) unset ($ stag [$ key]);
}
Foreach ($ stag as $ key => $ val)
{
If (in_array ($ val, array ('br ', 'img') unset ($ stag [$ key1]);
}
Array_reverse ($ stag );
$ Ends = '</'. implode ('> </', $ stag). '> ';
$ Re = substr ($ str, 0, $ I). $ ends;
If ($ more) $ re. = '...';
Return $ re;
}

PHP intercepts a string to generate a summary.
When writing a BLOG, we often need to display the first part of the article, but we are afraid of improper truncation and destruction of closed tags to cause damage to the entire document structure.

The Code is as follows: Copy code
Function text_zhaiyao ($ text, $ length) {// function for generating the Abstr $ test: Content $ length: Digest 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 current "<...>" 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;
}

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.