Home > Backend Development > PHP Problem > How to remove rich text style in php

How to remove rich text style in php

藏色散人
Release: 2023-03-12 19:20:01
Original
2791 people have browsed it

How to remove rich text style in php: 1. Open the corresponding file in the project; 2. Use the "function cutstr_html(){...}" method to remove all Html tag spaces; 3. Call "cutstr_html ” method can be used to remove text styles.

How to remove rich text style in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

How to remove rich text style in php?

php remove content formatting in rich text editor

##In a new project, I encounter the need to display the first few lines of plain text of the article content on the article list page. Because the background is an article added through a rich text editor, the data returned directly is formatted data, resulting in a list The page style is confusing, so the formatting needs to be removed to return plain text data.

/**
 * 去除Html所有标签、空格以及空白,并截取字符串(包括中文)
 * @param  string $string 字符串
 * @param  number $sublength 字符串长度
 * @param  string $encoding 编码方式
 * @param  string $ellipsis 省略号  
 */
 function cutstr_html($string,$sublength,$encoding = 'utf-8',$ellipsis = '…'){$string = strip_tags($string);
 $string = trim($string);
 $string = mb_ereg_replace("\t","",$string);
 $string = mb_ereg_replace("\r\n","",$string);
 $string = mb_ereg_replace("\r","",$string);
 $string = mb_ereg_replace("\n","",$string);
 $string = mb_ereg_replace(" ","",$string);
 if(mb_strlen(trim($string),&#39;utf-8&#39;) < $sublength){
         return trim($string).$ellipsis;
 }else{
         return mb_strcut(trim($string),0,$sublength,$encoding).$ellipsis;
 }}//测试字符串
 $str=&#39;<p style="microsoft yahei, arial; vertical-align: baseline; list-style-type: none; 
 text-indent: 28px; line-height: 25px; text-align:center;margin-top: 12px;
 margin-bottom: 12px; padding: 0px; border: 0px; font-size: 14px;color:red;">   
 fherfhewkolfjlkdsjfld</p>&#39;;
 //调用方法测试
 echo cutstr_html($string=$str,$sublength=5,$encoding=&#39;utf-8&#39;,$ellipsis=&#39;...&#39;);
Copy after login

The output is a plain text string of length 5, which can be displayed in the article list.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to remove rich text style in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template