Such old issue must have been fixed already. I love regexps and use it all the time, including preg_match(), sometimes with strings bigger than 200KB. Never had problems with it. It is not only the easiest way to parse and format data, but also the fastest one for a interpreted language like PHP.
A:
Havenard
2009-08-15 22:15:14
I have the latest PHP version - 5.3.0 (on Windows) - and the bug is still there. If I write the following code, PHP crashes. $string = str_repeat("a", 500); echo checkUTF8($string);
bilygates
2009-08-15 22:20:49
+1
A:
Have you tried ereg instead of preg_match? Perhaps this one doesn't have that bug, and you don't need a potentially buggy workaround.
Cheers,
Boldewyn
2009-08-15 22:16:36
I didn't try ereg, it might work, but I don't really want to use it because: "This function (ereg) has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged."
bilygates
2009-08-15 22:22:47
OK, but then you have quite a chance, that the preg_match bug is fixed in 6.0. Do a `if (function_exists('ereg'))` and use preg_match as fallback.
Boldewyn
2009-08-17 07:10:59
However, use one of the other suggestions. The one of Chacha102 is really fine, and since you use mb_substr in your example, I guess, you have MB string functions enabled. Dont forget to accept his (or any of the others') answer.
Boldewyn
2009-08-17 12:39:30
A:
Here is a string-function based solution:
http://www.php.net/manual/en/function.mb-detect-encoding.php#85294
troelskn
2009-08-15 23:26:17
+4
A:
You can always using the Multibyte String Functions:
If you want to use it a lot and possibly change it at sometime:
1) First set the encoding you want to use in your config file
/* Set internal character encoding to UTF-8 */
mb_internal_encoding("UTF-8");
2) Check the String
if(mb_check_encoding($string))
{
// do something
}
Or, if you don't plan on changing it, you can always just put the encoding straight into the function:
if(mb_check_encoding($string, 'UTF-8'))
{
// do something
}
Chacha102
2009-08-15 23:39:11