Home Backend Development PHP Tutorial PHP paginates long articles

PHP paginates long articles

Nov 21, 2016 pm 01:25 PM
php

The original intention of the design is: article list page. If you use strip_tags to delete all styles, the article introduction on the homepage will look messy and have no style. If you simply use strip_tags to retain p or br, then p or br will be truncated when you intercept the string.

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

The specific code is as follows: can be used directly

/**
* 长文章分段
* @param string $article 文章内容
* @param number $return_number 文章字节限制
* @return array
*/
private function ContentAddpage($content,$return_number=800){
    $return_content = "";    //返回的字符串
    $current_num = 0;        //当前字符串长度
    $return_content_num = 0; //上次字符串截取后的长度;用来处理最后数组中字数过长的case.
    $page_num_word = array();//以br为分隔符使用preg_split分隔字符串生成的数组;
    $content = strip_tags($content,'<p>,<br>');
    $content = preg_replace("/<p(.*?)>/m", "<br>",  $content);
    $content = str_replace("</p>","",  $content);
    $content = preg_replace("/<br(.*?)>/m", "<br>",  $content);
    $content_info = preg_split("/\<br\>/",$content);//根据字符串确定段落
    $art_num = count($content_info);//确定段落数
    for($i=0;$i <= $art_num-1;$i++){
        $page_num_word[$i] = strlen($content_info[$i]);
        $current_num += $page_num_word[$i];//得到字数
        if ($current_num <= $return_number){
            $tmp_num = $return_number-$current_num;
            $return_content .= mb_substr($content_info[$i],0,$tmp_num)."<br>";
            $return_content_num = $current_num;
        }else{
            $tmp_num = $return_number-$return_content_num;
            $return_content .= mb_substr($content_info[$i],0,$tmp_num)."<br>";
            break;
        }
    }
    return $return_content;
}
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles