Php shares functions that intercept strings containing html tags

Source: Internet
Author: User
This article mainly introduces php functions for intercepting strings containing html tags. it is more powerful than common string interceptions and is used in some special occasions, if you want to use this method, you can refer to the following: php intercepts the string containing html tags. if the tags are not closed, the tags are closed to prevent the unclosed tags from damaging the original webpage layout. After the screenshot is completed, the excess content is replaced with... or any other character. you can replace the specified content with the specified position by specifying the anchor.

/*** =========================================================== Intercept a string containing html tags = =======================================* @ param (string) $ str string to be truncated * @ param (int) $ lenth truncation length * @ param (string) $ replace the content exceeding the repalce with $ repalce (this parameter can be a string with html tags) * @ param (string) $ anchor captures the anchor, if you encounter this marking anchor during the interception process, it will end at the position of the anchor * @ return (string) $ result returned value * @ demo $ res = cut_html_str ($ str, 256 ,'... '); // Intercept 256 characters, and use '... 'replace * --------------------------------------------------------------------------------- * $ Author: Wang Jian. | Email: wj@yurendu.com | Date: ========================================================== = */function cut_html_str ($ str, $ lenth, $ replace = '', $ anchor ='
 ') {$ _ Lenth = mb_strlen ($ str, "UTF-8"); // The length of the statistical string (one character in both Chinese and English) if ($ _ lenth <= $ lenth) {return $ str; // The length of the input string is less than the truncation length and is returned as is} $ strlen_var = strlen ($ str ); // the length of the statistical string (UTF8 encoding-Chinese is counted as 3 characters, and English is counted as one character) if (strpos ($ str, '<') = false) {return mb_substr ($ str, 0, $ lenth); // contains no html tags and can be intercepted directly.} if ($ e = strpos ($ str, $ anchor )) {return mb_substr ($ str, 0, $ e); // contains the truncation mark, priority} $ html_tag = 0; // Mark the html code $ result = ''; // Summary string $ html_array = array ('left' => array (), 'right' => array (); // record the html tags that appear in the string after the interception, start => left, end => right/** if the string is:

AIf p is not closed, array is: array ('left' => array ('h3 ', 'P',' B '), 'right' => 'B', 'h3 '); * fill in html tags only, <? <% And other language tags will produce unpredictable results */for ($ I = 0; $ I <$ strlen_var; ++ $ I) {if (! $ Lenth) break; // after traversing, jump out of $ current_var = substr ($ str, $ I, 1); // The current character if ($ current_var = '<') {// The html code starts $ html_tag = 1; $ html_array_str = '';} else if ($ html_tag = 1) {// end an html code. if ($ current_var = '>') {$ html_array_str = trim ($ html_array_str); // remove spaces at the beginning and end, as shown in figure
and other possible leading and trailing spaces if (substr ($ html_array_str,-1 )! = '/') {// Determines whether the last character is/. If yes, the tag is closed and not recorded. // determines whether the first character is/. if Yes, put it in the right unit $ f = substr ($ html_array_str, 0, 1); if ($ f = '/') {$ html_array ['right'] [] = str_replace ('/', '', $ html_array_str); // Remove '/'} else if ($ f! = '? ') {// If Yes ?, PHP code, skip // if there is a halfwidth space, split by space, the first unit is the html tag. For example:

If (strpos ($ html_array_str ,'')! = False) {// It is divided into two units and may contain multiple spaces, for example, $ html_array ['left'] [] = strtolower (current (explode ('', $ html_array_str, 2);} else {// if there is no space, the entire string is an html tag, for example:

To lower case $ html_array ['left'] [] = strtolower ($ html_array_str) ;}}$ html_array_str = ''; // string reset $ html_tag = 0;} else {$ html_array_str. = $ current_var; // a string consisting of <> characters to extract html tags} else {-- $ lenth; // count non-html code} $ ord_var_c = ord ($ str {$ I}); switch (true) {case ($ ord_var_c & 0xE0) = 0xC0 ): // 2 bytes $ result. = substr ($ str, $ I, 2); $ I + = 1; break; case ($ ord_var_c & 0xF0) = 0xE0): // 3 bytes $ r Esult. = substr ($ str, $ I, 3); $ I + = 2; break; case ($ ord_var_c & 0xF8) = 0xF0): // 4 bytes $ result. = substr ($ str, $ I, 4); $ I + = 3; break; case ($ ord_var_c & 0xFC) = 0xF8): // 5 bytes $ result. = substr ($ str, $ I, 5); $ I + = 4; break; case ($ ord_var_c & 0xFE) = 0xFC): // 6 bytes $ result. = substr ($ str, $ I, 6); $ I + = 5; break; default: // 1 byte $ result. = $ current_var ;}}if ($ html_array ['left']) {// compare the left and right ht Ml labels. if not, add $ html_array ['left'] = array_reverse ($ html_array ['left']); // flip the left array, the supplemental order should be the opposite to the order in which html appears. foreach ($ html_array ['left'] as $ index => $ tag) {$ key = array_search ($ tag, $ html_array ['right']); // determines whether the label appears in the right if ($ key! = False) {// The unit unset ($ html_array ['right'] [$ key]) is deleted from right.} else {// does not appear, need to complete $ result. =' ';}} Return $ result. $ replace ;}

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.