note 95962 modified in function.mb-detect-encoding by danbrown

From: Date: Sun, 31 Jan 2010 23:51:07 +0000
Subject: note 95962 modified in function.mb-detect-encoding by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165533@lists.php.net to get a copy of this message
I was in the need of a function capable of detecting whether a string was ISO-8859-1 compatible or not. Unfortunately I was unable to get mb_detect_encoding to work. The following function does the trick. <?php function IsLatin1($str) { return (preg_match("/^[\\x00-\\xFF]*$/u", $str) === 1); } var_dump(IsLatin1("abc ABC 123")); // true var_dump(IsLatin1("abc € 123")); // false (because of €) ?> --was-- I was in the need of a function capable of detecting whether a string was ISO-8859-1 compatible or not. Unfortunately I was unable to get mb_detect_encoding to work. The following function does the trick. function IsLatin1($str) { return (preg_match("/^[\\x00-\\xFF]*$/u", $str) === 1); } var_dump(IsLatin1("abc ABC 123")); // true var_dump(IsLatin1("abc € 123")); // false (because of €) http://php.net/manual/en/function.mb-detect-encoding.php

« previous php.notes (#165533) next »