php怎样转换成html格式,php实现转换html格式为文本格式的方法
本文實例講述了php實現轉換html格式為文本格式的方法。分享給大家供大家參考,具體如下:
有時候需要轉換html格式的字符串為文本,但又需要保持一定的格式,比如要求段落變成的分段格式就可以用下面這個函數
function html2text($str){
$str = preg_replace("/
$str = preg_replace("/
$str = preg_replace("/
/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("/", "<", $str);
$str = preg_replace("/", "<", $str);
$str = preg_replace("/“/i", '"', $str);
$str = preg_replace("/&ldquo/i", '"', $str);
$str = preg_replace("/‘/i", "'", $str);
$str = preg_replace("/&lsquo/i", "'", $str);
$str = preg_replace("/'/i", "'", $str);
$str = preg_replace("/&rsquo/i", "'", $str);
$str = preg_replace("/>/i", ">", $str);
$str = preg_replace("/>/i", ">", $str);
$str = preg_replace("/”/i", '"', $str);
$str = preg_replace("/&rdquo/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.zyiz.net/code/phpformat
對于代碼及編碼轉換感興趣的朋友還可參考本站在線工具:
在線編碼轉換工具(utf-8/utf-32/Punycode/Base64):
在線UBB/HTML代碼轉換工具:
中文繁體字簡體字轉換(繁簡轉換)工具:
在線圖片轉換BASE64工具:
希望本文所述對大家PHP程序設計有所幫助。
總結
以上是生活随笔為你收集整理的php怎样转换成html格式,php实现转换html格式为文本格式的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php版本降级,wamp技巧之–升级降级
- 下一篇: 子网掩码是什么,可以做什么