Strip out HTML and Special Characters
I'd like to use any php function or whatever so that i can remove any HTML code and special characters and gives me only alpha-numeric output
$des = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";
I want the output become Hello world it s me and love you
(just Aa-Zz-0-9-WhiteSpace)
I've tried strip_tags
but it removes only HTML codes
$clear = strip_tags($des);
echo $clear;
So is there any way to do it?
Solution 1:
Probably better here for a regex replace
// Strip HTML Tags
$clear = strip_tags($des);
// Clean up things like &
$clear = html_entity_decode($clear);
// Strip out any url-encoded stuff
$clear = urldecode($clear);
// Replace non-AlNum characters with space
$clear = preg_replace('/[^A-Za-z0-9]/', ' ', $clear);
// Replace Multiple spaces with single space
$clear = preg_replace('/ +/', ' ', $clear);
// Trim the string of leading/trailing space
$clear = trim($clear);
Or, in one go
$clear = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($des))))));
Solution 2:
Strip out tags, leave only alphanumeric characters and space:
$clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($des));
Edit: all credit to DaveRandom for the perfect solution...
$clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags(html_entity_decode($des)));