I know that my answer comes very late, but I thought it could help someone else. I believe the best way to extract all special characters is to use utf8_decode () in php. Even for working with or any other special character using space, use utf8_decode() .
After using utf8_decode() you can manipulate these characters directly in the code. For example, in the following code, the clean () function replaces empty. Then it replaces all the extra spaces with a single space using preg_replace() . Leading and trailing spaces are removed using trim() .
function clean($str) { $str = utf8_decode($str); $str = str_replace(" ", "", $str); $str = preg_replace("/\s+/", " ", $str); $str = trim($str); return $str; } $html = " Hello world! lorem ipsum."; $output = clean($html); echo $output;
Hello World! lorem ipsum.
Sriram rangathathan
source share