Home >Backend Development >PHP Tutorial >php Convert HTML to text_PHP tutorial
php
// $document should contain an HTML document.
// This example will remove HTML tags and javascript code
// and whitespace characters. We will also add some common
// HTML entities are converted into corresponding text.
$search = array ("''si", // Remove javascript
"'<[/!]*?[^<>]*?>'si", // Remove the HTML tag
"'([rn])[s]+'", // Remove whitespace characters
"'&(quot|#34);'i", // Replace HTML entity
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'(d+);'e"); // Run as PHP code
$replace = array ("",
"",
"1",
""",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(1)");
$text = preg_replace ($search, $replace, $document);
?>