Half-size check with regular expression
Represent other than half-width characters in [^…] part.
$value = "こんにちは"; echo preg_match('/^[^ -~。-°\x00-\x1f\t]+$/u', $value);
[vagrant@localhost tests]$ ls
index.php
[vagrant@localhost tests]$ php -S 192.168.35.10:8000
PHP 7.1.21 Development Server started at Fri Feb 8 20:56:23 2019
Listening on http://192.168.35.10:8000
Document root is /home/vagrant/local/app/tests
Press Ctrl-C to quit.
[Fri Feb 8 20:56:31 2019] PHP Warning: preg_match(): Compilation failed: range out of order in character class at offset 11 in /home/vagrant/local/app/tests/index.php on line 5
なに? 修正します。
$value = "123"; if (preg_match('/^[^ -~。-゚\x00-\x1f\t]+$/u', $value)){ echo "yes"; } else { echo "半角です"; }
/u
→ UTF-8 character code
OK