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?
This should do what you're looking for: function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. } Hope it helpss!!
stripHtml( html ) Changes the provided HTML string into a plain text string by converting <br> , <p> , and <div> to line breaks, stripping all other tags, and converting escaped characters into their display values.
The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.
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))))));
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)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With