Home · Latest · Trends  /  PHP
3

Convert HTML to plain text in PHP

rated 3 times [  3] [ 0]  / comments: 0 / hits: 3133  / 1 year ago, sat, sep 30, 2017, 01:28:06
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' => "",
  '/&nbsp;/si' => " ",
  '/&middot;/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;
?>
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
 
Share
Snippets
Log and display all errors, warnings and notices into a file
1 year ago, sat, sep 16, 2017
Posted
giles

giles

Member since Sep 16, 2017
Total Code Snippets: 2
Total Comments: 0
Location: n/a
Following
User not following anyone yet.