Jump to content

Need help with str_replace


JuanDBB

Recommended Posts

Hello,

 

Sorry if this is too simple, but I'm becoming crazy with this. I want to change special characters like á, é, é, ó ú and ñ for a,e,i,o,u, n

 

I'm doing this:

 

function friendlyName($name) {

    $name = strtolower($name);

 

    $name = str_replace(" ", "", $name);

 

    $find = array('á', 'é', 'í', 'ó', 'ú', 'ñ');

    $repl = array('a', 'e', 'i', 'o', 'u', 'n');

    $name = str_replace ($find, $repl, $name);

 

    return $name;

}

 

BUT this doesn't work. It doesn't replace anything. If the name is "La Coruña" it returns "lacoruña" or if it's "León" it returns "león"

 

Anybody has an idea about what may be happening?

 

Thanks a lot in advance!

Link to comment
Share on other sites

Odd. The function works for me. Must be something to do with the character encoding for the page.

 

However, here is a more complete list of character replacements. (found it here: http://www.randomsequence.com/articles/removing-accented-utf-8-characters-with-php/)

 

<?php

function friendlyName($name)
{
    $find = explode(","," ,ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u");
    $repl = 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");
    return str_replace ($find, $repl, strtolower($name));
}

echo friendlyName("La Coruña"); //Output: lacoruna
echo friendlyName("León"); //Output: leon

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.