dpbfb7119 2017-01-14 22:02
浏览 40
已采纳

PHP preg_match不允许æøå

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?

  • 写回答

1条回答 默认 最新

  • dongmale0656 2017-01-14 22:41
    关注

    If your PHP file is already UTF-8 encoded, you should tell your database, that you need 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.

    This is an example for a mysqli connection object:

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

    Afterwards your queries will return UTF-8 encoded results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?