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

Advanced XPATH – Nested predicates

Extensible Markup Language ( XML ) is a standard way to store and share information , describing data and to focus on what data is.

Xpath is a language that makes possible find information in an XML document. This language has a huge importance in the W3C’s XSLT standard and is the base of Xquery and Xpointer. With Xpath we can navigate through elements and it’s attributes. The wide use of XML and the importance of Xpath turns it in a fundamental knowledge in the ITs world.

In this post I’ll answer a friend that has an advanced question. However, I will try to give some basic explanations for those who don’t want to study XML and Xpath from the beginning.

If you want to learn Xpath I recommend the Xpath tutorial from wschools.

Let’s start showing the data that I will use to answer. The information shown bellow is only for the example.

<?xml version=”1.0″ encoding=”UTF-8″?>
<store>
<product>
<identification>
<full_name>Laptop</full_name>
<model>XGSB-2</model>
</identification>
<price>1000</price>
<available value=”yes” />
</product>

<product>
<identification>
<full_name>Desktop</full_name>
<model>DRHD-2</model>
</identification>
<price>500</price>
<available value=”yes” />
</product>

<product>
<identification>
<full_name>Desktop</full_name>
<model>DRHD-3</model>
</identification>
<price>600</price>
<available value=”no” />
</product>
</store>

The information shown below is a XML content that has some elements structured like a tree of nodes. In XML there are many nodes, like elements, attributes, text, and others.

Now is time to think in my friend’s question. He wants to get all identification information from the products that are available.

After see the XML content let’s check how to navigate through it and get the information that we want. Xpath has some expressions to select nodes lile:

Expression Description
Nodename Selects all child nodes of the named node
/ Selects from the root node
// Selects nodes from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes

With the expressions above we can build the path to the identification information that we want.

/store/product/identification

This expression can be built in other ways and returns all product identification nodes.

Now we use one predicate to select only the products that are available, this information is in an attribute of the available element. The basic use of predicates is apply a condition to an element or an element that is a sibling like the current one, e.g. an element is the same level of the current. The doubt is that we want the attribute of an element that isn’t in the direct navigation path and we will to use the elements name and check it’s attribute with another predicate.

We can put a predicate insider another one? Yes. We will use it on this example.

The predicate applies to an element or a parent element or a sibling element. Its position in the query affects the expression in the predicate. In this case, we must be aware that the element in the predicates expression shouldn’t be a full path because using one, we check if there is some available element, inclusive from another product, with its value attribute set to true.

Then, using tow predicates we have the following query: /store/product[available[@value=”true”]]/identification

Xpath have functions that can be use to solve some problems. In this one, the use of a predicate inside another solves it without have to use functions.

To check this example working I used PHP and its DOM extension to work with XML. A very easy way of navigate and find information in a XML document. Check the source code below:

<?
echo “Starting checking available products <br />”;

$xml = new DOMDocument();
$xml->load(‘products.xml’);

$xpath = new DOMXPath($xml);
$products = $xpath->query(‘/store/product[available[@value=”yes”]]/identification’);

for ($i = 0; $i < $products->length; $i++)
{
if ($products->item($i) && !$products->item($i)->hasChildNodes()) continue;

$childs = $products->item($i)->childNodes;

echo “<br />Full name: “.$childs->item(1)->nodeValue.”<br />”;
echo “Model: “.$childs->item(3)->nodeValue.”<br />”;
}
?>

Amazing Honda campaign

While surfing the web, especially in YouTube, I found a great commercial spot from Honda. This spot has something like 2 years, but I think it’s amazing and I want to share it with you.

This spot is not an interactive web campaign neither some marketing innovation technique but a simple and really good TV spot.

Honda is a great car manufacturer but they are showing that cars aren’t the only thing that they do good. Besides the old spot above, there is a really cool one recently published to promote Honda Civic.

I love Japanese cars and Honda ones are really great. Their quality and the fantastic campaigns that they made help Honda to still one of the most known brands.

Honda – The power of dreams

Cool web campaigns as an evolution of marketing

With the evolution of the Internet and virtual world the marketing has become in new and interactive solutions that capture the watcher attention and incentive him to participate.

Web Marketing is a new age where brands can be more close to the client and surround him in the world of its campaigns. The interactivity offered by Rich Interactive Applications (RIA), available in the new information age, turns the client from a watcher to a player or a character that participates in the campaign.

The possibility to interact in the campaign allows the watcher to easily recommend the campaign to his friends, making the term “viral marketing” a reality with a publicity made by your own clients and/or visitors.

In the last year there were several campaigns that reflect this new age of marketing. Here I’ll show and describe different types of campaigns made in 2007.

Na tua casa ou na minha? – Montepio Geral (Portugal)

Na tua casa ou na minha? - Montepio

A Portuguese bank called “Montepio Geral” launched a campaign to show how simple its mortgages solutions are. They intended to build a TV and web campaign.

The first step was to choose a good slogan and the one was “Na tua casa ou na minha?” (“In your place or in mine?”), trying to let the watchers mind fly to sexual intention, at least in the Portuguese expression. With the slogan and a piece of the campaign, a provocative piece, they launched a web campaign exploring the viral marketing by showing online the campaign even before the TV to all visitors that have seen and recommend the web site. The video of the campaign was published in the YouTube being available to the public and making part of the web campaign.

The web site of the campaign is no longer online but the URL http://www.natuacasaounaminha.com takes the visitor to the mortgages page of the bank.

Gamebox – Sporting (Portugal)

In this campaign we enter in a more interactive world, where the Gamebox web campaign from the world wide known soccer team Sporting, allows the web site visitor to be part of the campaign.

Gamebox - Sporting

The “Gamebox” campaign asks your name and phone number and starts the campaign movie. The movie shows all players, some of them international and shows the coach waiting for someone. After a while the coach makes a phone call. For your surprise the visitors phone (the same you gave to the campaign) starts ringing. When you answer, the coach, Paulo Bento, start talking with you saying that they’re waiting for you and your presence is important. If you don’t answer the phone, the behavior of the coach is different.

This type of interactive campaign is a way to mark the visitor and call his attention to the product, even if he don’t buy, he will not forget it so soon.

The interactive version of the campaign is no longer available but the standard trailer still at: http://www.sporting.pt/Servicos/Gamebox/Gameboxvideo.asp

“Fight for kisses” – Wilkinson (International)

The last example is a fully interactive campaign with a game, a trailer and some goodies to enter visitors mind and shows the product advantages. A campaign based on a full history, have a meaning and a purpose. This campaign started in 2007 but still going this year.

This campaign is a fully Rich Interactive Application (RIA) that shows the baby’s fighter world. It has a story, a really good movie animation campaign, some goodies and a great game that allows the visitor to fight for a cause and take the campaign spirit.

Fight for kisses - Wilkinson

Check more about this campaign on another post of this blog.