无法使用 php 读取数组中带重音符号的名称

cannot read accented name in array with php

我想检查某个名称是否已存在于数组中。 我对包含重音字符的名称有疑问。 下面是使用的代码,当填写(法语)姓名 Charlène Rodriês 和(德语)姓名 Jürgen Günter 时;它输出:不存在.

如何捕捉这些包含重音字符的名称?

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['bioname'])) {
        $bioname = trim(htmlentities($_POST['bioname']));
        
        $array = array('John Doe','Bill Cleve','Charlène Rodriês','мария преснякова','Jürgen Günter');
        
        if (in_array($bioname_raw, $array)) { // if bioname already exists
            echo '<div">'.$bioname.' ALREADY exists!</div>';

        }
        else {
            echo '<div">'.$bioname.' NOT exists!</div>';
        }   
    }
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">    
    <input class="form-control" name="bioname" type="text" placeholder="AUTHORNAME">    
    <button type="submit" id="cf-submit" name="submit" class="btn btn-primary w-100">POST</button>                                  
</form>

你在比较苹果和橘子。

当您执行 htmlentities('Charlène Rodriês') 时,它会更改字符串并将其编码为:Charl&egrave;ne Rodri&ecirc;s,这显然与您的 in_array() 中的 Charlène Rodriês 不匹配。

因此,当您从 $_POST 变量中获取值时,请删除 htmlentities()

$bioname = trim($_POST['bioname']);

并且仅在输出数据之前使用该函数:

 echo '<div">'. htmlentities($bioname).' ALREADY exists!</div>';

根据一般经验,不要对输入数据进行编码。仅在您使用数据时对数据进行编码,因为不同的用例需要不同类型的编码。