Removing Accented UTF-8 Characters with PHP


13 Apr Removing Accented UTF-8 Characters with PHP

If you ever worked on a non UTF-8 encoded website then chances that you have had problems with special chars, and I’m not talking here about how to deal with those for the database, but rather an even bigger problem related with file uploads and choosing a file name with accented characters.

Just had the same issue working on a project for a client where I had a script that dynamically creates folders and uploads files, well I decided to try a weird accented name to create a folder, and the results were shocking, the folder was created, and no FTP software was able to deal with it, couldn’t neither rename it nor delete it, i had to do it from the CPanel and this little situation had urged me to create a function that skips the accented characters and here it is:

function escape_chars($data) {
		$search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u");
		$replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u");
		$data = str_replace($search, $replace, $data);
		$data = stripslashes($data);
		$data = mysql_real_escape_string($data);
		$data = str_replace("'", '', $data);
		$data = str_replace('"', '', $data);
		$data = str_replace('\\', '', $data);
		$data = str_replace(' ', '_', $data);
		$data = str_replace('-', '_', $data);
		$data = urlencode($data);
		return $data;

Please note that we used:

$data = mysql_real_escape_string($data); 

This might cause a problem for some servers and the solution is simply to connect to database before calling a MySQL function

Any ways, that’s about it! i hope this helps someone else avoid hours of frustrations and headache, drop me a line if you need any help and don’t forget to share the post on if you found it useful, someone else might too.

  • peptide
    Posted at 03:46h, 28 April Reply

    I appreciate your insightful post. great information. I hope you produce more. I will carry on watching

Post A Comment