1

I want to do a check if a name already exists in an array. I have an issue with a name which contains accented chars. Below is the code is use and when filling in the (french) name Charlène Rodriês and the (german) name Jürgen Günter; it outputs: NOT exists.

How can i catch these names which contain accented characters?

<?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>
john
  • 1,263
  • 5
  • 18

1 Answers1

1

You're comparing apples and oranges.

When you do htmlentities('Charlène Rodriês'), it changes the string and will encode it into: Charl&egrave;ne Rodri&ecirc;s, which obviously won't match Charlène Rodriês in your in_array().

So remove the htmlentities() when you get the value from the $_POST-variable:

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

and only use that function before you output the data:

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

As a general rule of thumb, don't encode data on input. Only encode data when you use it since different use cases requires different types of encoding.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40