如何在放入数据库之前将表单中输入的特殊字符转换为相似的英文字符?
e.g. we have form that ask for first name last name country, country is drop down list, but name is free entry. We need to make sure we have no special characters in input field. And instead convert them to english. e.g.
Á -> A,á -> a,Č -> C,č -> c,ď -> d,é -> e,ě -> e,É -> E,Ě -> E,í -> i,Í
-> i,Ň -> N,ň -> n,Ó -> O,ó -> o,Ř -> R,ř -> r,Š -> S,š -> s,ť -> t,Ú -> U,ú -> u,Ů -> U,ů -> u,
Ý -> Y,ý -> y,Ž -> Z,ž -> z
Is there some way to do it in php or mysql?
例如。 我们有要求名字姓氏国家,国家是下拉名单的形式,但是名字是免费入场。 我们需要确保输入字段中没有特殊字符。 而是将它们转换为英语。 e.g。 p>
Á - > A,á - > a,Č - > C,č - > c,ď - > d,é - > e,ě - > e,É - > E,Ě - > E,í - > I,I \正> 我,Ň - > N,ü - > n,Ó - > O,ó - > o,Ř - > R,ř - > r,Š - > S,š - > s,ť - > t,Ú - > U,ú - > 你,Ů - > 你,...... - > 你,
Ý - > Y,ý - > y,Ž - > Z,ž - > z
code> pre>
有没有办法在php或mysql中做到这一点? p>
div>
Use php function utf8_encode
Or detect encoding and change it if it is not utf-8, like below:
<?php
//$s is a string from whatever source
mb_detect_encoding($s, "UTF-8") == "UTF-8" ? : $s = utf8_encode($s);
?>
If you can use PHP >= 5.3 then normalizer.normalize()
should do the job. By the way, the name for what you want to do is removing diacritics. If you are using an earlier version of PHP then you should be able to use the same function if you enable the intl PHP extension (as commented here How to remove diacritics from text?)
Edit: Found a related question How do I remove accents from characters in a PHP string?