How can I use strlen in php for Persian?

try this:

function ustrlen($text)
{
    if(function_exists('mb_strlen'))
        return mb_strlen( $text , 'utf-8' );
    return count(preg_split('//u', $text)) - 2;
}

it will work for any php version.


mb_strlen function is your friend


Use mb_strlen

Returns the number of characters in string str having character encoding (the second parameter) encoding. A multi-byte character is counted as 1.

Since your 3 characters are all multi-byte, you get 6 returned with strlen, but this returns 3 as expected.

echo mb_strlen($string,'utf-8');

Fiddle

Note

It's important not to underestimate the power of this method and any similar alternatives. For example one could be inclined to say ok if the characters are multi-byte then just get the length with strlen and divide it by 2 but that will only work if all characters of your string are multi-byte and even a period . will invalidate the count. For example this

echo mb_strlen('علی.','utf-8');

Returns 4 which is correct. So this function is not only taking the whole length and dividing it by 2, it counts 1 for every multi-byte character and 1 for every single-byte character.

Note2:

It looks like you decided not to use this method because mbstring extension is not enabled by default for old PHP versions and you might have decided not to try enabling it:) For future readers though, it is not difficult and its advisable to enable it if you are dealing with multi-byte characters as its not only the length that you might need to deal with. See Manual