Crazy tips to remove spacial character using PHP

Crazy tips to remove spacial character using PHP

Sometimes we are facings spacial character issue while working on any other language except English. PHP programmers you can easily manage English URLs and use them as parameter. But if we trying to push any spacial character like Spanish, Banglai or any other language in URL section then sometimes applications not working properly. Today we are going to know some crazy tips about how to remove spacial characters and replace them with regular English character.

– What are the special characters are?

There are various kinds of spacial characters over the world. For example purpose I’m going to show you how to replace Spanish characters. So, in Spanish language some spacial characters are – “á,é,í,ó,ú,ñ” and so one.

– What we are going to replace?

We are going to replace following spacial characters with regular English characters. Have a look replacement list –

á into a

é into e

í into i

ó into o

ú into u

ñ into n

– Special Characters Replace Solution:

PHP provide a great function called str_replace for sting replacement. It’s takes 3 parameters for replacement task. First parameters use for search words/characters, second one use for replacement words and last one for full text. You can pass search word lists as parameter. I’ll show you both example in below.

var $text = "Ãtletico Clube Goianiense";
$replace_characters = array("à", "á", "â", "ã", "ä", "å");
//Output: atletico Clube Goianiense
//Replace: Ã
$text = str_replace($replace_characters, "a", $text);

$text = "Liværpool Fútbol Club";
//Output: Livaerpool Futbol Club
//Replace: æ
$text = str_replace("æ", "ae", $text);

$text = "Liverpool Futbol çlub";
//Output: Livaerpool Futbol Club
//Replace: ç
$text = str_replace("ç", "c", $text);

$text = "Livèrpool Futbol Club";
//Output: liverpool futbol cluba
//Replace: è
$replace_characters = array("è", "é", "ê", "ë");

$replace_characters = array("ì", "í", "î", "ï");
$text = "Lîverpool Futbol Club";
//Output: Liverpool Futbol Club
//Replace: î
$text = str_replace($replace_characters, "i", $text);

$text = "Club Cerro Porteño";
//Output: Club Cerro Porteno
//Replace: ñ
$text = str_replace("ñ", "n", $text);

$replace_characters = array("ò", "ó", "ô", "õ", "ö");
$text = "Club Deportivo Unión Comercio";
//Output: Club Deportivo Union Comercio
//Replace: ó
$text = str_replace($replace_characters, "o", $text);

$text = "Club Depœtivo Unión Comercio";    
//Output: Club Depoertivo Union Comercio
//Replace: œ
$text = str_replace("œ", "oe", $text);

$text = "Liverpool Fútbol Club";	
$replace_characters = array("ù", "ú", "û", "ü");    
//Output: Liverpool Futbol Club
//Replace: ú
$text = str_replace($replace_characters, "u", $text);

$text = "ýouth futbol Club";
$replace_characters = array("ý", "ÿ");    
//Output: Youth Futbol Club
//Replace: ý
$text = str_replace($replace_characters, "y", $text);

– Compact Version:

Now, I’m going to show you a compact version. Here goes snippet.

$text = "Montpellier Hérault Sport Club";

$seaching_character = array(
        "/[À-Å]/","/Æ/","/Ç/",
        "/[È-Ë]/","/[Ì-Ï]/",
        "/Ð/","/Ñ/","/[Ò-ÖØ]/",
        "/×/","/[Ù-Ü]/","/[Ý-ß]/",
        "/[à-å]/","/æ/","/ç/",
        "/[è-ë]/","/[ì-ï]/","/ð/",
        "/ñ/","/[ò-öø]/","/÷/",
        "/[ù-ü]/","/[ý-ÿ]/"
);
$replace_character = array("A","AE","C","E","I","D","N","O",
                    "X","U","Y","a","ae","c","e","i","d",
                    "n","o","x","u","y"
                    );

echo preg_replace($seaching_character,$replace_character, $text);

You can create it as a function and pass require parameters according to your need.

– Twitter API does not support spacial characters in URL:

Last day working on twitter authentication system and found that twitter API does not support any spacial character containing URL for their authentication process. Searched over the internet and all suggested to replace spacial characters with regular English characters. I’m not sure what kind of problem twitter face with spacial characters. Lot’s of people uses twitter from different area of the world. They should take an initiative to solve spacial character issue in URL section.

In that case I’ve converted all URLs in my application and replace spacial characters with regular English characters. Though it’s not good enough for SEO and I’m going to loose my SEO quality. But I’ve nothing to do!

Hope this tutorial will help you to solve spacial character issue. Please add your valuable comments and suggestion in our comment section.

Enjoy!

Share It

Related Posts