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 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 and the code is as follows:

 ] +?> )~ 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 ("~
 ] *?> ~ 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 a Chinese string * @ param $ start position * @ param $ length * @ param $ charset encoding * @ param $ dot additional string */ function msubstr ($ string, $ start, $ length, $ dot = '', $ charset = 'utf-8') {$ string = str_replace (array ('&','"', '<', '>', ''), 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) {$ t N = 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;}/*** gets 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 ;}?>

The code is as follows:

 = $ Leng) return $ str; $ word = 0; $ I = 0;/** string pointer **/$ stag = array ()); /** mark for storing start HTML **/$ etag = array ();/** mark for storing end 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;} (; $ 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 ='
 
 '; $ Re = substr ($ str, 0, $ I ). $ ends; if ($ more) $ re. = '... '; return $ re ;}?>

PHP intercepts the string to generate the abstract of the article. when writing a BLOG, we often need to display the first part of the article, but we are afraid that the closed tag is not properly truncated to cause the entire document structure to be damaged. the code is as follows:

 ] * (> ?) "; $ 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;}?>


Address:

Reprinted at will, but please attach the article address :-)

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.