Php intercepts html strings and automatically complements html tags. This article mainly introduces how php intercepts html strings and automatically complements html tags, php is a very practical technique for html operations. php intercepts html strings and automatically complements html tags.
This article mainly introduces how php intercepts html strings and automatically complements html tags. it is a very practical technique of php for html operations and has some reference value. For more information, see
This example describes how php intercepts html strings and automatically complements html tags. Share it with you for your reference. The specific analysis is as follows:
Here, we will summarize the use of php to capture html strings and automatically complete html tags. in actual development, we often encounter this problem. many people directly filter 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:
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 (! Emptyempty ($ 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;
}
The code is as follows:
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 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:
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;
}
I hope this article will help you with php programming.
This article describes how php intercepts html strings and automatically complements html tags. it is a very useful technique for php html operations...