public static function convertToPlainText($text, $includeAHrefs = true, $includeImgAlts = true)
{
// remove tabs, line feeds and carriage returns
$text = str_replace(array("\t", "\n", "\r"), '', $text);
// remove the head-, style- and script-tags and all their contents
$text = preg_replace('|\\<head[^>]*\\>(.*\\n*)\\</head\\>|isU', '', $text);
$text = preg_replace('|\\<style[^>]*\\>(.*\\n*)\\</style\\>|isU', '', $text);
$text = preg_replace('|\\<script[^>]*\\>(.*\\n*)\\</script\\>|isU', '', $text);
// put back some new lines where needed
$text = preg_replace('#(\\<(h1|h2|h3|h4|h5|h6|p|ul|ol)[^\\>]*\\>.*\\</(h1|h2|h3|h4|h5|h6|p|ul|ol)\\>)#isU', "\n\$1", $text);
// replace br tags with newlines
$text = preg_replace('#(\\<br[^\\>]*\\>)#isU', "\n", $text);
// replace links with the inner html of the link with the url between ()
// eg.: <a href="http://site.domain.com">My site</a> => My site (http://site.domain.com)
if ($includeAHrefs) {
$text = preg_replace('|<a.*href="(.*)".*>(.*)</a>|isU', '$2 ($1)', $text);
}
// replace images with their alternative content
// eg. <img src="path/to/the/image.jpg" alt="My image" /> => My image
if ($includeImgAlts) {
$text = preg_replace('|\\<img[^>]*alt="(.*)".*/\\>|isU', '$1', $text);
}
// decode html entities
$text = html_entity_decode($text, ENT_QUOTES, 'ISO-8859-15');
// remove space characters at the beginning and end of each line and clear lines with nothing but spaces
$text = preg_replace('/^\\s*|\\s*$|^\\s*$/m', '', $text);
// strip tags
$text = strip_tags($text, '<h1><h2><h3><h4><h5><h6><p><li>');
// format heading, paragraphs and list items
$text = preg_replace('|\\<h[123456]([^\\>]*)\\>(.*)\\</h[123456]\\>|isU', "\n** \$2 **\n", $text);
$text = preg_replace('|\\<p([^\\>]*)\\>(.*)\\</p\\>|isU', "\$2\n", $text);
$text = preg_replace('|\\<li([^\\>]*)\\>\\n*(.*)\\n*\\</li\\>|isU', "- \$2\n", $text);
// replace 3 and more line breaks in a row by 2 line breaks
$text = preg_replace('/\\n{3,}/', "\n\n", $text);
// use php constant for new lines
$text = str_replace("\n", PHP_EOL, $text);
// trim line breaks at the beginning and ending of the text
$text = trim($text, PHP_EOL);
// return the plain text
return $text;
}