3
Convert HTML to plain text in PHP
Convert HTML to formatted plain text with PHP
<?php
/**
* totext()
*
* @param mixed $content
* @return
*/
function totext($content) {
$pregarray = array(
'/(<a\b[^>]*>|<\/a>)/si' => "",
'/\r/si' => "",
'/\n/si' => "",
'/ /si' => " ",
'/·/si' => " ",
'/<(img)\b[^>]*alt=\"([^>"]+)\"[^>]*>/si' => "",
'/<(img)\b[^>]*>/si' => "",
'/<head\b[^>]*>.*?<\/head>/si' => "",
'/<script\b[^>]*>.*?<\/script>/si' => "",
'/<style\b[^>]*>.*?<\/style>/si' => "",
'/<div\b[^>]*>\>/si' => "",
'/<nav\b[^>]*>.*?<\/nav>/si' => "",
'/<footer\b[^>]*>.*?<\/footer>/si' => "",
'/<source\b[^>]*>.*?<\/source>/si' => "",
'/<(source)\b[^>]*>/si' => "",
'/<\/?(div|b|span|dt|dt|nav|dd|li|table|tr|article|header|td|ul|ol|dl|aside|section|main|p|h1|h2|h3|h4|h5|h6)[^>]*\>/si' => " ",
'/<form(.*?)<\/form>/si' => "",
'/<input\b[^>]*>/si' => "");
$content = preg_replace(array_keys($pregarray), array_values($pregarray), $content);
$content = strip_tags($content);
$content = preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", $content));
return $content;
}
?>
<?php
$content = 'My page content';
$content = totext($content);
echo $content;
?>
rated 3 times
(3)
(0)
comments: 0 / hits: 4169
/ 4 years ago, sat, sep 30, 17, 01:28:06
More From
» PHP
Comments
There are no comments for this Snippet yet
Only authorized users can post. Please sign in first, or register a free account