strspn et les caractères UTF-8

Published on 27/09/2016  PHP

Pour trouver le premier caractère d'une chaîne UTF-8 qui n'est pas défini dans une liste de caractères, on peut utiliser le code :
>
<?php

$allowedChars 
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
                    
"abcdefghijklmnopqrstuvwxyz".
                    
"éèàïùçàÉÈÀÏÙÇÀ+.-_'@ \r\n".
                    
"0123456789";
preg_match('/^['.$allowedChars.']+/u'$value$matches);
if (
count ($matches))
{
  
$pos mb_strlen ($matches[0]);
  
$error sprintf (
           
_("Invalid character provided : '%s' (position %d)"),
           
mb_substr ($values["visitorNames"], $pos1), $pos);
}