| Viewed 105 times

How to get the number of the string in PHP for non-English


How to get the number of the string in PHP for non-English

PHP
1 Answer
Mohammad Qandeel
Answered
9

it`s important to check the real length of the string.

The strlen() function will return the number of bits, not the number of characters

you can use mb_strlen() to get the number of characters. A multi-byte character is counted as 1

Example 

<?php
    echo mb_strlen('نعم','utf8').'<br>'; // 3 char
    echo strlen('نعم').'<br>'; // 6 char 
?>

You can use this function to check if the string Written in English or other languages

<?php

function is_english($str) {
    if (strlen($str) == mb_strlen($str,'utf8')){
        return 1;
    }else {
        return -99;
    } 
}
echo is_english('OK').'<br>';
echo is_english('نعم').'<br>';

?>