简体   繁体   English

PHP preg_match 不允许 æøå

[英]PHP preg_match will not allow æøå

I am trying to make my registration script accept only letters and æøå, but nothing I've tried so far has worked.我试图让我的注册脚本只接受字母和 æøå,但到目前为止我尝试过的一切都没有奏效。 It still won't accept æøå.它仍然不会接受 æøå。

This is my code so far到目前为止,这是我的代码

if ($_POST['first'] != "" && preg_match("%^[-a-zA-Z æøå\/]+$%", $_POST['first']) == true && strlen($_POST['first']) > 0) { 
    $input_first = mysqli_real_escape_string($con,$_POST['first']); } 
else { header("Location: ../register.php?n=2"); exit; die(); }

What am I doing wrong?我究竟做错了什么?

If your PHP file is already UTF-8 encoded, you should tell your database, that you need UTF-8.如果您的 PHP 文件已经是 UTF-8 编码,您应该告诉您的数据库,您需要 UTF-8。 Instead of fiddling with the configurations of MySQL, just tell your connection object, which character-set you expect, the database does the rest for you.不要摆弄 MySQL 的配置,只需告诉您的连接对象,您期望使用哪种字符集,数据库会为您完成其余的工作。

This is an example for a mysqli connection object:这是一个 mysqli 连接对象的例子:

 //Test-ak
  $db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
  $db->set_charset("utf8");

Afterwards your queries will return UTF-8 encoded results.之后,您的查询将返回 UTF-8 编码的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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