23
04
Removing special characters with PHP using iconv

When we dealing with the user data may be from any of the website or from csv or excel style sheet some non utf-8 special characters will get added up with normal characters. To handle this situation PHP has a inbuilt function which we called as iconv.. Actually its a very huge library having different functions, but here we will we see a simple and easy example which will help you to remove the special character in your data.

Examples - To remove special character with php

$input = "Fóø Bår Zacarías ?S?B?D Ferreíra"; // original text
 
$output = iconv("utf-8", "ascii//TRANSLIT//IGNORE", $input);
 
$output =  preg_replace("/^'|[^A-Za-z0-9\s-]|'$/", '', $output); // lets remove utf-8 special characters except blank spaces
 
echo $output;

Output

Foo Bar Zacarias ASABAD Ferreira

References

http://www.php.net/manual/en/function.iconv.php

By posted on - 23rd Apr 2016