/**
* Gets plain text for a given text
*
* @return string
* @param string $text
* @param bool[optional] $includeAHrefs
* @param bool[optional] $includeImgAlts
*/
function getPlainText($text, $includeAHrefs = false, $includeImgAlts = false)
{
// 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('|\
]*\>(.*\n*)\|isU', '', $text);
$text = preg_replace('|\|isU', '', $text);
$text = preg_replace('|\|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('#(\
]*\>)#isU', "\n", $text);
// replace links with the inner html of the link with the url between ()
// eg.: My site => My site (http://site.domain.com)
if($includeAHrefs) $text = preg_replace('|(.*)|isU', '$2 ($1)', $text);
// replace images with their alternative content
// eg.
=> My image
if($includeImgAlts) $text = preg_replace('|\
]*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, '
');
// format heading, paragraphs and list items
$text = preg_replace('|\]*)\>(.*)\|isU', "\n** $2 **\n", $text);
$text = preg_replace('|\]*)\>(.*)\
|isU', "$2\n", $text);
$text = preg_replace('|\]*)\>\n*(.*)\n*\|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 contant 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;
}