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>
You're comparing apples and oranges.
When you do
htmlentities('Charlène Rodriês')
, it changes the string and will encode it into:Charlène Rodriês
, which obviously won't matchCharlène Rodriês
in yourin_array()
.So remove the
htmlentities()
when you get the value from the $_POST-variable:and only use that function before you output the data:
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.