php html转换text的方法:首先创建一个PHP示例文件;然后定义一个html2text方法;最后通过“preg_replace”实现格式匹配即可。

推荐:《PHP视频教程》
网页中拖动 DIV 是很常见的操作,今天就分享给大家一个 jQuery 多列网格拖动布局插件,和其它的插件不太一样的地方在于你处理拖放的元素支持不同大小,并且支持多列的网格布局,它们会自动的根据位置自己排序和调整。非常适合你开发具有创意的应用。这个插件可以帮助你将任何的 HTML 元素转换为网格组件
74
有时候需要转换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("/&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;
}
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号