Php paging long articles

Source: Internet
Author: User
The original intention of the design is: the article list page. if you delete all styles with strip_tags, the articles on the homepage will look messy and have no styles. If you simply use strip_tags to retain p or br, p or br may be truncated when you intercept the string. The design concept is: retain p and br, then replace p with br, and then use br as the separator to separate the entire article. The code is as follows: you can directly use **... the original intention of the design is: the article list page. if you delete all styles with strip_tags, the articles on the homepage will look messy and have no styles. If you simply use strip_tags to retain p or br, p or br may be truncated when you intercept the string.

The design concept is: retain p and br, then replace p with br, and then use br as the separator to separate the entire article.

The code is as follows:

/*** Long article segmentation * @ param string $ article content * @ param number $ return_number article byte limit * @ return array */private function ContentAddpage ($ content, $ return_number = 800) {$ return_content = ""; // The Returned string $ current_num = 0; // The length of the current string $ return_content_num = 0; // The length after the last string truncation; it is used to process cases with long words in the last array. $ page_num_word = array (); // use preg_split as the separator to separate the array generated by the string; $ content = strip_tags ($ content ,'

,
'); $ Content = preg_replace ("/ /M ","
", $ Content); $ content = str_replace ("

"," ", $ Content); $ content = preg_replace ("/ /M ","
", $ Content); $ content_info = preg_split ("/\ /", $ Content); // Determine the paragraph according to the string $ art_num = count ($ content_info); // determine the number of paragraphs for ($ I = 0; $ I <= $ art_num-1; $ I ++) {$ page_num_word [$ I] = strlen ($ content_info [$ I]); $ current_num + = $ page_num_word [$ I]; // Obtain the number of words if ($ current_num <= $ return_number) {$ tmp_num = $ return_number-$ current_num; $ return_content. = mb_substr ($ content_info [$ I], 0, $ tmp_num )."
"; $ Response = $ current_num;} else {$ tmp_num = $ return_number-$ return_content_num; $ return_content. = mb_substr ($ content_info [$ I], 0, $ tmp_num )."
"; Break ;}} return $ return_content ;}

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.