Php intercepts html strings and automatically supplements html tags _ PHP Tutorial

Source: Internet
Author: User
Php intercepts html strings to automatically complete html tags. The article will summarize the use of php to capture html strings and automatically complete html tags. in actual development, this will often happen. many people directly filter out html tags by strip_tags, however, there is only one article to summarize the issue of using php to capture html strings to automatically complete html tags. in actual development, many people often encounter this issue. many people directly filter html tags by strip_tags, however, only plain text is left, and the readability is very poor. Below is a function
The code is as follows:

/**
* 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) = "") 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 (! Empty ($ tagStack )){
$ Result. =' ';
}
Return $ result;
}

/**
* Truncate a Chinese string
* @ Param $ string
* @ Param $ start bit
* @ 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 ){
$ 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 );
$ Length = count ($ ar [0]);
}
Return $ length;
}

Instance

The code is as follows:

* @ 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 =' ';
$ 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:
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.