php实现转换html格式为文本格式的方法_php技巧

本文实例讲述了php实现转换html格式为文本格式的方法。分享给大家供大家参考,具体如下:

有时候需要转换html格式的字符串为文本,但又需要保持一定的格式,比如要求段落变成的分段格式就可以用下面这个函数

function html2text($str){
 $str = preg_replace("/<style .*?<\\/style>/is", "", $str);
 $str = preg_replace("/<script .*?<\\/script>/is", "", $str);
 $str = preg_replace("/<br \\s*\\/>/i", ">>>>", $str);
 $str = preg_replace("/<\\/?p>/i", ">>>>", $str);
 $str = preg_replace("/<\\/?td>/i", "", $str);
 $str = preg_replace("/<\\/?div>/i", ">>>>", $str);
 $str = preg_replace("/<\\/?blockquote>/i", "", $str);
 $str = preg_replace("/<\\/?li>/i", ">>>>", $str);
 $str = preg_replace("/ /i", " ", $str);
 $str = preg_replace("/ /i", " ", $str);
 $str = preg_replace("/&/i", "&", $str);
 $str = preg_replace("/&/i", "&", $str);
 $str = preg_replace("/</i", "<", $str);
 $str = preg_replace("/</i", "<", $str);
 $str = preg_replace("/“/i", '"', $str);
 $str = preg_replace("/“/i", '"', $str);
 $str = preg_replace("/‘/i", "'", $str);
 $str = preg_replace("/‘/i", "'", $str);
 $str = preg_replace("/'/i", "'", $str);
 $str = preg_replace("/’/i", "'", $str);
 $str = preg_replace("/>/i", ">", $str);
 $str = preg_replace("/>/i", ">", $str);
 $str = preg_replace("/”/i", '"', $str);
 $str = preg_replace("/”/i", '"', $str);
 $str = strip_tags($str);
 $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
 $str = preg_replace("/.*?;/i", "", $str);
 return $str;
}

PS:小编在这里推荐一款本站的php格式化美化的排版工具帮助大家在以后的PHP程序设计中进行代码排版:
 
php代码在线格式化美化工具:http://tools.jb51.net/code/phpformat

对于代码及编码转换感兴趣的朋友还可参考本站在线工具:

在线编码转换工具(utf-8/utf-32/Punycode/Base64):

http://tools.jb51.net/transcoding/decode_encode_tool

在线UBB/HTML代码转换工具:

http://tools.jb51.net/transcoding/ubb2html

中文繁体字简体字转换(繁简转换)工具:

http://tools.jb51.net/transcoding/convertzh

在线图片转换BASE64工具:

http://tools.jb51.net/transcoding/img2base64

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php
, 转换
, 文本格式
HTML格式
文本格式转换为数字、把日期格式转换成文本、excel转换成文本格式、文本格式转换器、文本格式转换,以便于您获取更多的相关知识。

时间: 2024-08-22 14:22:57

php实现转换html格式为文本格式的方法_php技巧的相关文章

php发送html格式文本邮件的方法_php技巧

本文实例讲述了php发送html格式文本邮件的方法.分享给大家供大家参考.具体实现方法如下: <?php $to = "simon@mailexample.com, elaine@mailexample.com"; //设置收件人 $subject = "This is a test"; //设置E-mail主题 //设置E-mail内容: $message = " <html> <head> <title>Thi

详解WordPress中简码格式标签编写的基本方法_php技巧

WordPress 简码是一种类似于论坛标签的东西,格式类似于把尖括号换成中括号的 Html 标签.简码很多人叫做短代码,但官方的翻译应该是简码,在这里纠正一下. 简码的开发的逻辑比较简单,主要就是添加.删除和判断,会在本文全部介绍. 简码格式 简码的格式非常灵活,可以是有属性.无属性.闭合.非闭合等等: [example] [example]内容[/example] [example attr="属性" attr-hide="1"]内容[/example] [ex

php实现二进制和文本相互转换的方法_php技巧

本文实例讲述了php实现二进制和文本相互转换的方法.分享给大家供大家参考.具体如下: 这段代码包含两个函数,bin2text,二进制转换为文本,text2bin,文本转换成二进制 <?php function bin2text($bin_str) { $text_str = ''; $chars = explode("\n",chunk_split(str_replace("\n",'',$bin_str),8)); $_I = count($chars);

php实现把url转换迅雷thunder资源下载地址的方法_php技巧

本文实例讲述了php实现把url转换迅雷thunder资源下载地址的方法.分享给大家供大家参考.具体方法分析如下: 如果你知道迅雷地址的生成规则你就不觉得迅雷的url资源下载地址有多么复杂了, 其实雷的地址就是原url前面带AA后面带BB之后再base64_encode编码即可 如下例子所示: 复制代码 代码如下: <?php function Thunder($url, $type='en') { $url ='http://www.jb51.net'; if($type =='en'){ r

php转换颜色为其反色的方法_php技巧

本文实例讲述了php转换颜色为其反色的方法.分享给大家供大家参考.具体分析如下: 这段php代码可以把一个颜色变成与之相反的颜色编码,如:白色变成黑色,蓝色变成黄色 function color_inverse($color){ $color = str_replace('#', '', $color); if (strlen($color) != 6){ return '000000'; } $rgb = ''; for ($x=0;$x<3;$x++){ $c = 255 - hexdec(

php使用wordwrap格式化文本段落的方法_php技巧

本文实例讲述了php使用wordwrap格式化文本段落的方法.分享给大家供大家参考.具体分析如下: wordwrap()函数可以按照指定的固定行长度格式化文本段落,让段落看起来更加整齐 <?php $string = "TRADING ON MARGIN POSES ADDITIONAL RISKS AND IS NOT SUITABLE FOR ALL INVESTORS. A COMPLETE LIST OF THE RISKS ASSOCIATED WITH MARGIN TRADI

php简单判断文本编码的方法_php技巧

本文实例讲述了php简单判断文本编码的方法.分享给大家供大家参考.具体如下: 这里通过对文本的一次循环编码,来判断是否属于该编码. public function chkCode($string) { $code = array( 'ASCII', 'GBK', 'UTF-8' ); foreach ($code as $c) { if ($string === iconv('UTF-8', $c, iconv($c, 'UTF-8', $string))) { return $c; } } r

PHP数据集构建JSON格式及新数组的方法_php技巧

自己写了个PHP结果集转换成JSON格式的函数,可以直接调用: 复制代码 代码如下: function RecordToJson($recordset) { $jstr='['; while($rs = $recordset->Fetch()) { //$nick = iconv("GBK",'utf-8',$rs['nick']);/*转换为utf-8编码*/ //TODO:遍历结果集 $arr_keys=array_keys($rs); $jstr=$jstr.'{'; fo

PHP JSON格式的中文显示问题解决方法_php技巧

返回json数据中文显示的问题 上一篇文章中,返回json格式的中文显示成\u5723\u8bde\u8282\u5343\u4e07\u597d\u793c\u5927\u5949\u9001 解决方法一: 复制代码 代码如下: <?php function Notice(){     include './include/conn.php';  //数据库链接文件     $sql_notice = mysql_query('SELECT * FROM gg_notice where ena

phpmyadmin里面导入sql语句格式的大量数据的方法_php技巧

sql.php文件的内容如下: 复制代码 代码如下: <? system("mysql -uroot -pmypassword database < a.sql"); print "sql ok"; ?> 其中root表示数据库用户名,mypassword表示root的密码,database表示要导入的数据库的名字,a.sql表示数据库文件的文件名