Why does this preg_replace to remove HTML entities not work?
// Remove all HTML entities
$text = preg_replace('/&[A-Za-z0-9]+?;/',' ', $text);
I’m simply trying to replace all HTML entities like ( &###; , < , and etc. ) with spaces, but I seem to be missing something because it is not replacing them and I’m utterly confused now.
Test case
Code:
// Remove all HTML entities
$title="♥♥♥ I like cats ♥♥♥";
echo "BEFORE : ".$title."\n";
$title2 = preg_replace('/&[A-Za-z0-9]+?;/e',' ', $title);
echo "AFTER : ".$title2."\n";
Output:
BEFORE : ♥♥♥ I like cats ♥♥♥
AFTER : ♥♥♥ I like cats ♥♥♥
PHP Info:
PHP Version: 5.3.6-13 ubuntu 3.5
Regex Library: Bundled library enabled
You’re missing
#It should be this RegEx instead in your preg_replace call: