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
https://forums.phpfreaks.com/topic/187647-need-help-with-str_replace/
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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.