Utilizzando i dati di esempio e alcuni caratteri cinesi &, la regex /[!\p{Common}\p{Latin}]*/iu
sembra funzionare. Per maggiori dettagli, questo sito ha uno sfondo piacevole su predefined Unicode categories e un bel simple explanation here. Ma restituisce una corrispondenza vuota per i caratteri non latini in questa versione. L'uno nella mia EDIT: in fondo sarà meglio per la logica booleana pulita:
// Set a test array.
$test_array = array();
$test_array[] = 'JóhnDoe';
$test_array[] = 'Fübar';
$test_array[] = 'Lòrem';
$test_array[] = 'FírstNäme';
$test_array[] = '•••••••';
$test_array[] = 'Çákë';
$test_array[] = '形声字/形聲字';
$test_array[] = 'ラドクリフ、マラソン';
// Set the header for debugging output.
header('Content-Type: text/plain; charset=utf-8');
// Roll through the test array.
foreach ($test_array as $test_value) {
// Run a regex to detect latin and common characters.
preg_match('/[!\p{Common}\p{Latin}]*/iu', $test_value, $matches);
// Kludge using array filtering to get rid of empty matches.
$matches = array_filter($matches);
// Dump the matches for debugging.
print_r($matches);
}
L'uscita è la seguente: Si noti come i cinesi & caratteri giapponesi tornano partite vuote. Notare anche come il corrispondesse allo \p{Common}
corrispondenza regex. Se non si desidera che vengano visualizzati caratteri comuni come questo, è sufficiente modificare la regex per essere, /[!\p{Latin}]*/iu
. Sto usando array_filter
per pulire quei valori vuoti, ma è kludgy. Quindi questo non è perfetto, ma utile:
Array
(
[0] => JóhnDoe
)
Array
(
[0] => Fübar
)
Array
(
[0] => Lòrem
)
Array
(
[0] => FírstNäme
)
Array
(
[0] => •••••••
)
Array
(
[0] => Çákë
)
Array
(
)
Array
(
)
EDIT: Questo codice test utilizza una variante del regex ho postato qui sopra per eliminare il problema non-partita vuoto sopra; /(?:[\p{Latin}])+/iu
. Nota che funziona solo corrispondendo allo \p{Latin}
quindi \p{Common}
non giocherà bene qui. Ma i risultati utilizzando /(?:[\p{Latin}])+/iu
sono più puliti e garantiscono è possibile utilizzare un semplice controllo booleano di preg_match
per verificare la presenza di caratteri invece di dover utilizzare un kludge che mescola la matrice $matches
con array_filter
:
// Set a test array.
$test_array = array();
$test_array[] = 'JóhnDoe';
$test_array[] = 'Fübar';
$test_array[] = 'Lòrem';
$test_array[] = 'FírstNäme';
$test_array[] = '•••••••';
$test_array[] = 'Çákë';
$test_array[] = '形声字/形聲字';
$test_array[] = 'ラドクリフ、マラソン';
// Set the header for debugging output.
header('Content-Type: text/plain; charset=utf-8');
// Roll through the test array.
foreach ($test_array as $test_value) {
// Run a regex to detect latin and common characters.
preg_match('/(?:[\p{Latin}])+/iu', $test_value, $matches);
// Dump the matches for debugging.
print_r($matches);
}
E i nuovi risultati sono i segue.Si noti che le matrici vuote sono veramente vuote e il prey_match
restituirà un valore booleano false
in quei casi:
Array
(
[0] => JóhnDoe
)
Array
(
[0] => Fübar
)
Array
(
[0] => Lòrem
)
Array
(
[0] => FírstNäme
)
Array
(
)
Array
(
[0] => Çákë
)
Array
(
)
Array
(
)
Avete dati di input di esempio da confrontare? – JakeGould
[[: alpha:]] non lo risolve? –
@AfonsoTsukamoto Provalo. Corrisponderà comunque a cinesi e caratteri giapponesi. – JakeGould