PHP replacing special characters like à->a, è->e

前端 未结 8 1768
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 14:20

I have php document signup.php which save the content from form (in form.php document) to MySQL base. The problem arises when I want to reformat the input content. I want do

8条回答
  •  春和景丽
    2020-11-27 15:07

    There's a much easier way to do this, using iconv - from the user notes, this seems to be what you want to do: characters transliteration

    // PHP.net User notes
    
    

    Be very conscientious with your character encodings, so you are keeping the same encoding at all stages in the process - front end, form submission, encoding of the source files. Default encoding in PHP and in forms is ISO-8859-1, before PHP 5.4 where it changed to be UTF8 (finally!).

    There's a couple of functions you can play around with for ideas. First is from CakePHP's inflector class, called slug:

    public static function slug($string, $replacement = '_') {
        $quotedReplacement = preg_quote($replacement, '/');
    
        $merge = array(
            '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
            '/\\s+/' => $replacement,
            sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
        );
    
        $map = self::$_transliteration + $merge;
        return preg_replace(array_keys($map), array_values($map), $string);
    }
    

    It depends on a self::$_transliteration array which is similar to what you were doing in your question - you can see the source for inflector on github.

    Another is a function I use personally, which comes from here.

    function slugify($text,$strict = false) {
        $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
        // replace non letter or digits by -
        $text = preg_replace('~[^\\pL\d.]+~u', '-', $text);
    
        // trim
        $text = trim($text, '-');
        setlocale(LC_CTYPE, 'en_GB.utf8');
        // transliterate
        if (function_exists('iconv')) {
            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        }
    
        // lowercase
        $text = strtolower($text);
        // remove unwanted characters
        $text = preg_replace('~[^-\w.]+~', '', $text);
        if (empty($text)) {
            return 'empty_$';
        }
        if ($strict) {
            $text = str_replace(".", "_", $text);
        }
        return $text;
    }
    

    What those functions do is transliterate and create 'slugs' from arbitrary text input, which is a very very useful thing to have in your toolchest when making web apps. Hope this helps!

提交回复
热议问题