php html如何转换 text-PHP问题

资源魔 46 0

php html转换text的办法:起首创立一个PHP示例文件;而后界说一个html2text办法;最初经过“preg_replace”完成格局婚配便可。

保举:《PHP视频教程》

php html转换 text

有时分需求转换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 html若何转换 text的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 html php故障解决 php使用问题 text

抱歉,评论功能暂时关闭!