Home  >  Article  >  Backend Development  >  How to convert php htm to txt

How to convert php htm to txt

藏色散人
藏色散人Original
2021-06-08 09:03:432094browse

php htm转为txt的方法:首先创建一个PHP示例文件;然后通过“preg_replace ($search, $replace, $document);”方式将HTML转换成文本即可。

How to convert php htm to txt

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php htm怎么转为txt?

PHP将HTML转换成文本的实现代码

 

核心代码:

<?php
// $document 应包含一个 HTML 文档。
// 本例将去掉 HTML 标记,javascript 代码
// 和空白字符。还会将一些通用的
// HTML 实体转换成相应的文本。
 
$search = array ("&#39;<script[^>]*?>.*?</script>&#39;si", // 去掉 javascript
         "&#39;<[\/\!]*?[^<>]*?>&#39;si",      // 去掉 HTML 标记
         "&#39;([\r\n])[\s]+&#39;",         // 去掉空白字符
         "&#39;&(quot|#34);&#39;i",         // 替换 HTML 实体
         "&#39;&(amp|#38);&#39;i",
         "&#39;&(lt|#60);&#39;i",
         "&#39;&(gt|#62);&#39;i",
         "&#39;&(nbsp|#160);&#39;i",
         "&#39;&(iexcl|#161);&#39;i",
         "&#39;&(cent|#162);&#39;i",
         "&#39;&(pound|#163);&#39;i",
         "&#39;&(copy|#169);&#39;i",
         "&#39;&#(\d+);&#39;e");          // 作为 PHP 代码运行
 
$replace = array ("",
         "",
         "\\1",
         "\"",
         "&",
         "<",
         ">",
         " ",
         chr(161),
         chr(162),
         chr(163),
         chr(169),
         "chr(\\1)");
 
$text = preg_replace ($search, $replace, $document);
?>

推荐学习:《PHP视频教程

The above is the detailed content of How to convert php htm to txt. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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