Clean file name in PHP with Regular Expressions

In Web developing there are some main features and many concerns that we all must be aware. The file uploading is one of the important and indispensible features that we certainly will use sooner or later.

I’m sure that many of you have had troubles when uploading files that have names with special chars, especially if we run our application in a Linux SO. In fact, using Linux we have to be aware of case sensitive names and special chars. In Windows, we usually don’t have this problem and we can name our files without many limitations. This problem has a higher importance in Latin countries like mine (Portugal), here is normal to name files like “Activação Telefónica.doc”.

So, I decided to code a simple function that cleans the file name and post it here to be sure that when I need it again I known were this little simple but useful function is. I use the PHP function preg_replace to replace special chars with standard ones. This is only an example and don’t clean all special chars. You should add more chars to the filter by simple adding more arrays to the cleaner one. See the function bellow:


<?
$txt = "Activação Telefónica.jpg";

function cleanFileName( $str )
{
$cleaner = array();
$cleaner[] = array('expression'=>"/[àáäãâª]/",'replace'=>"a");
$cleaner[] = array('expression'=>"/[èéêë]/",'replace'=>"e");
$cleaner[] = array('expression'=>"/[ìíîï]/",'replace'=>"i");
$cleaner[] = array('expression'=>"/[òóõôö]/",'replace'=>"o");
$cleaner[] = array('expression'=>"/[ùúûü]/",'replace'=>"u");
$cleaner[] = array('expression'=>"/[ñ]/",'replace'=>"n");
$cleaner[] = array('expression'=>"/[ç]/",'replace'=>"c");

$str = strtolower($str);
$ext_point = strripos($str,”.”); // Changed to strripos to avoid issues with ‘.’ Thanks nico.
if ($ext_point===false) return false;
$ext = substr($str,$ext_point,strlen($str));
$str = substr($str,0,$ext_point);

foreach( $cleaner as $cv ) $str = preg_replace($cv[“expression”],$cv[“replace”],$str);

return preg_replace(“/[^a-z0-9-]/”,”_”,$str).$ext;
}

echo cleanFileName( $txt );
?>

I hope that this function will be useful to you.

MIDI to MP3 Converter & Shareware Software

While searching and testing for good technical posts, here is another opinion one. Today I was searching for free software for convert MIDI to MP3. Yes, the old MIDI files because I want to have music in a website but I don’t want to violate copyrights. Then I found a website that has some free music with authorization to use. However they are, some of them, MIDI files.

Every time I search for a software to use one time or rarely, I’ve lots of problems like the lack of freeware to do it, shareware software that are commercial but work worst than free solution, and, the most “pain in the ass” the software classified as shareware that don’t fit the term shareware and don’t allow us to use it, allowing only convert a percent of the file or convert but don’t save the result.

Hopefully there are some companies and some products that don’t take us for fouls and don’t want money to use one time or to really test the software. In my journey to convert a MIDI file to MP3 I found the product Easy MIDI Converter from AudioTool.net. They allow us to convert 30 files, in a maximum of 5 in the same time, with good quality and a very simple interface without to configure hundreds of options like other solutions. It solved my problem. Despite I don’t want to buy the software I liked it and went to its website and I found some other cool features. This is what I think that shareware is for.

Thanks to AudioTool.net

Amazing thin notebook

Recently Apple has presented MacBook Air. This amazing thin notebook is a really good piece of work and proves that we can do better in many technology matters. I always listen all people say, Japan is known because they can take a product and reinvent it. This time, Apple shown that they can reinvent. They’ve transformed a standard notebook in really portable device maintaining all its key features by exploring and rethinking the existent and tested high tech.

Greetings to Apple for its good job and its amazing product. Now I hope that other great notebook manufacturers like Sony and Toshiba, that I like pretty much, follow the example.

Here stay this amazing thin computer.

MacBook Air - Thin notebook

See more on Apple website