問題描述
我有 php 文檔 signup.php,它將表單(在 form.php 文檔中)的內容保存到 MySQL 基礎.當我想重新格式化輸入內容時會出現問題.我想解碼 UTF-8 字符,如 à->a.
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 decode UTF-8 charachters like à->a.
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$course=$_POST['course'];
$chain="prêt-à-porter";
$pattern = array("'é'", "'è'", "'?'", "'ê'", "'é'", "'è'", "'?'", "'ê'", "'á'", "'à'", "'?'", "'a'", "'?'", "'á'", "'à'", "'?'", "'?'", "'?'", "'ó'", "'ò'", "'?'", "'?'", "'ó'", "'ò'", "'?'", "'?'", "'í'", "'ì'", "'?'", "'?'", "'í'", "'ì'", "'?'", "'?'", "'ú'", "'ù'", "'ü'", "'?'", "'ú'", "'ù'", "'ü'", "'?'", "'y'", "'?'", "'Y'", "'?'", "'?'", "'?'", "'?'", "'?'", "'?'", "'?'");
$replace = array('e', 'e', 'e', 'e', 'E', 'E', 'E', 'E', 'a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'A', 'o', 'o', 'o', 'o', 'O', 'O', 'O', 'O', 'i', 'i', 'i', 'I', 'I', 'I', 'I', 'I', 'u', 'u', 'u', 'u', 'U', 'U', 'U', 'U', 'y', 'y', 'Y', 'o', 'O', 'a', 'A', 'A', 'c', 'C');
$chain = preg_replace($pattern, $replace, $chain);
echo $chain; // print pret-a-porter
$first_name = preg_replace($pattern, $replace, $first_name);
echo $first_name; // does not change the input!?!
為什么它對 $chain 非常有效,但對 $first_name 或 $last_name 不起作用?
Why it works perfectly for $chain, but for $first_name or $last_name doesnt work?
我也試試
echo $first_name; // print áááááábééééééb????
$trans = array("á" => "a", "é" => "e", "?" => "s");
echo strtr("áááááábééééééb????", $trans); // print aaaaaabeeeeeebssss
echo strtr($first_name,$trans); // print áááááábééééééb????
但正如你所見,問題是一樣的!
but the problem, as you can see, is same!
推薦答案
有一個更簡單的方法,使用 iconv
- 從用戶注釋來看,這似乎是你想要做的: 字符音譯
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
<?php
$string = "?ABBāSāBāD";
echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $string);
// output: [nothing, and you get a notice]
echo iconv('UTF-8', 'ISO-8859-1//IGNORE', $string);
// output: ABBSBD
echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
// output: ABBASABAD
// Yay! That's what I wanted!
?>
要非常認真地處理您的字符編碼,以便在流程的所有階段保持相同的編碼 - 前端、表單提交、源文件的編碼.PHP 和表單中的默認編碼是 ISO-8859-1,在 PHP 5.4 之前它更改為 UTF8(終于!).
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!).
有幾個函數可以讓您發揮創意.第一個來自 CakePHP 的 inflector 類,叫做 slug
:
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(
'/[^sp{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);
}
這取決于 self::$_transliteration
數組,它與您在問題中所做的類似 - 您可以 查看 github 上的 inflector 源.
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('~[^\pLd.]+~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;
}
這些函數的作用是從任意文本音譯和創建slugs"輸入,這是制作 Web 應用程序時工具箱中非常有用的東西.希望這會有所幫助!
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!
這篇關于PHP 替換特殊字符,如 à->a、è->e的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!