Replacing accented characters php

后端 未结 19 1410
鱼传尺愫
鱼传尺愫 2020-11-22 16:03

I am trying to replace accented characters with the normal replacements. Below is what I am currently doing.

    $string = \"Éric Cantona\";
    $strict = st         


        
19条回答
  •  一向
    一向 (楼主)
    2020-11-22 16:43

    So I found this on php.net page for preg_replace function

    // replace accented chars
    
    $string = "Zacarías Ferreíra"; // my definition for string variable
    $accents = '/&([A-Za-z]{1,2})(grave|acute|circ|cedil|uml|lig);/';
    
    $string_encoded = htmlentities($string,ENT_NOQUOTES,'UTF-8');
    
    $string = preg_replace($accents,'$1',$string_encoded);
    

    If you have encoding issues you may get someting like this "Zacarías Ferreíra", just decode the string and use said code above

    $string = utf8_decode("Zacarías Ferreíra");
    

提交回复
热议问题