Removing Accented UTF-8 Characters with PHP

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.

Abdelkader Soudani

About Abdelkader Soudani

Abdelkader Soudani is the Founder and Director of Sproweb International, He is also a web developer/designer who specializes in front and back end development, Iphone and Android development, marketing and video production. If you'd like to connect with him then head over to the contact page or his profile on Google Plus.

One comment on “Removing Accented UTF-8 Characters with PHP

  1. peptide says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>