Php 从HTML创建纯文本
我正在开发一个函数,该函数将使用php将HTML转换为纯文本版本Php 从HTML创建纯文本,php,html,strip-tags,Php,Html,Strip Tags,我正在开发一个函数,该函数将使用php将HTML转换为纯文本版本 $html='<style type="text/css"> @media only screen and (max-width: 480px) { .message_mobile { width: 100% !important; } } </style> <p class="message_mobile"> sample Text</p&g
$html='<style type="text/css">
@media only screen and (max-width: 480px) {
.message_mobile {
width: 100% !important;
}
}
</style>
<p class="message_mobile"> sample Text</p>';
$plain_text =strip_tags($html);
echo $plain_text;
但是我不需要
标签中的内容。如何做到这一点?
我还有另一个问题,当我试图用一张表剥离标签时,它会产生不需要的线路制动器。如何解决这些问题?
有没有从HTML创建纯文本的好方法?使用此功能:
<?php
function strip_html_tags($str){
$str = preg_replace('/(<|>)\1{2}/is', '', $str);
$str = preg_replace(
array(// Remove invisible content
'@<head[^>]*?>.*?</head>@siu',
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
),
"", //replace above with nothing
$str );
$str = replaceWhitespace($str);
$str = strip_tags($str);
return $str;
} //function strip_html_tags ENDS
//To replace all types of whitespace with a single space
function replaceWhitespace($str) {
$result = $str;
foreach (array(
" ", " \t", " \r", " \n",
"\t\t", "\t ", "\t\r", "\t\n",
"\r\r", "\r ", "\r\t", "\r\n",
"\n\n", "\n ", "\n\t", "\n\r",
) as $replacement) {
$result = str_replace($replacement, $replacement[0], $result);
}
return $str !== $result ? replaceWhitespace($result) : $result;
}
$html='<style type="text/css">
@media only screen and (max-width: 480px) {
.message_mobile {
width: 100% !important;
}
}
</style>
<p class="message_mobile"> sample Text</p>';
$plain_text = strip_html_tags($html);
echo $plain_text;
您要查找的函数是
此代码:
<?php
$htmltag = '
<style type="text/css">
@media only screen and (max-width: 480px) {
.message_mobile {
width: 100% !important;
}
}
</style>
<p class="message_mobile"> sample Text</p>';
echo "<pre>".nl2br(htmlspecialchars($htmltag))."</pre>";
?>
将在您的网站上创建此输出:
@仅介质屏幕和(最大宽度:480px){
.message_mobile{
宽度:100%!重要;
}
}
您可以使用类从HTML创建纯文本
请访问此链接,这可能会对您有所帮助。
课程:
试试这个,它对我有帮助
请检查此项one@Jenz在html2text的帮助下,我的问题解决了。谢谢
<?php
$htmltag = '
<style type="text/css">
@media only screen and (max-width: 480px) {
.message_mobile {
width: 100% !important;
}
}
</style>
<p class="message_mobile"> sample Text</p>';
echo "<pre>".nl2br(htmlspecialchars($htmltag))."</pre>";
?>
<style type="text/css">
@media only screen and (max-width: 480px) {
.message_mobile {
width: 100% !important;
}
}
</style>
<p class="message_mobile"> sample Text</p>