PHP to intercept the string containing the HTML tag to share the _php instance of the function

Source: Internet
Author: User
Tags code tag html code tag html tags ord strlen

This method works by intercepting a string containing an HTML tag and closing the tag if it is not closed to prevent the open label from breaking the original page layout. After the interception, replace the extra content with the ... or any other character that supports an anchor point designation to be replaced to the specified location.

* * ============================== intercepts strings containing HTML tags ========================= * @param (String) $str to intercept the string * @param (i NT) $lenth Intercept length * @param (string) $repalce the excess content replaced with $repalce (this parameter can be a string with HTML tags) * @param (String) $anchor intercept the anchor point, if the interception process To this tag anchor, the return value * @demo $res = cut_html_str ($STR, 256, ' ... ') at the anchor point * @return (String) $result Intercept 256 lengths, and replace the remainder with ' ... ' *-------------------------------------------------------------------------------* $	Author:wang Jian.	| | DATE:2014/03/16 * ===============================================================================/function Cut_ Html_str ($str, $lenth, $replace = ', $anchor = ' <!--break--> ') {$_lenth = Mb_strlen ($str, "utf-8");//Statistics string length (Chinese, English 	is counted as a character) if ($_lenth <= $lenth) {return $str;//The passed-in string length is less than intercept length, returned as is $strlen _var = strlen ($STR);  Statistic string Length (UTF8 code-Chinese 3 characters, English count a character) if (Strpos ($str, ' < ') = = False) {return mb_substr ($str, 0, $lenth);//No HTML included tag, Direct intercept} if ($e = Strpos ($str, $anchOR) {return mb_substr ($str, 0, $e);//include truncation flag, priority} $html _tag = 0; 	HTML code tag $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/* * such as the string:  

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: 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.