A: 

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.

Havenard
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
+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
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
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
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
+1  A: 

You should be able to use iconv to check for validity. Just try and convert it to UTF-16 and see if you get an error.

derobert
A: 

Here is a string-function based solution:

http://www.php.net/manual/en/function.mb-detect-encoding.php#85294

troelskn
+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
+1, the MB string functions are made for such a task.
Boldewyn
A: 

hi i cant use this because sum chars make it faile

mehdi
I've fixed it. There were several wrong ifs there. You should try it now.
bilygates