简体   繁体   中英

str_replace in php not working with special characters

I am trying the following code to replace all spanish special characters to something that can be converted to an URL.

<?php
                $Handle = "blusa-tipo-túnica-asimétrica-sin-mangas";
                $Handle = str_replace( 'à', 'a', $Handle );
                $Handle = str_replace( 'á', 'a', $Handle );
                $Handle = str_replace( 'â', 'a', $Handle );
                $Handle = str_replace( 'ã', 'a', $Handle );
                $Handle = str_replace( 'ä', 'a', $Handle );
                $Handle = str_replace( 'å', 'a', $Handle );
                $Handle = str_replace( 'è', 'e', $Handle );
                $Handle = str_replace( 'é', 'e', $Handle );
                $Handle = str_replace( 'ê', 'e', $Handle );
                $Handle = str_replace( 'ë', 'e', $Handle );
                $Handle = str_replace( 'ì', 'i', $Handle );
                $Handle = str_replace( 'í', 'i', $Handle );
                $Handle = str_replace( 'î', 'i', $Handle );
                $Handle = str_replace( 'ï', 'i', $Handle );
                $Handle = str_replace( 'ð', 'o', $Handle );
                $Handle = str_replace( 'ñ', 'n', $Handle );
                $Handle = str_replace( 'ò', 'o', $Handle );
                $Handle = str_replace( 'ó', 'o', $Handle );
                $Handle = str_replace( 'ô', 'o', $Handle );
                $Handle = str_replace( 'õ', 'o', $Handle );
                $Handle = str_replace( 'ù', 'u', $Handle );
                $Handle = str_replace( 'ú', 'u', $Handle );
                $Handle = str_replace( 'û', 'u', $Handle );
                $Handle = str_replace( 'ü', 'u', $Handle );;
                
                echo $Handle;
?>

But the above prints exactly the same input I give "blusa-tipo-túnica-asimétrica-sin-mangas". Why? What am I doing wrong?

My little trick to replace all special characters is to convert the string to HTML, then replace the special characters by their base letters:

function strip_accents($str)
    {
        $str = htmlentities($str, ENT_COMPAT, 'UTF-8');
        
        $str = preg_replace('#\&([A-za-z])(?:acute|cedil|circ|grave|ring|tilde|uml)\;#', '\1', $str);
        $str = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $str);
        $str = preg_replace('#\&[^;]+\;#', '', $str);
        
        return $str;
    }

Note: make sure your source file is UTF-8 encoded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM