1

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

我怎样才能捕捉到这些包含重音字符的名称?

<?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>
4

1 回答 1

1

你在比较苹果和橘子。

当您这样做时htmlentities('Charlène Rodriês'),它会更改字符串并将其编码为: Charl&egrave;ne Rodri&ecirc;s,这显然与Charlène Rodriês您的in_array().

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

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

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

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

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

于 2020-09-20T10:31:42.733 回答