+4 votes
in Programming Languages by (6.3k points)
I am fetching the URL and title of youtube videos. The title of some videos contains English and non-English characters. How can I check if the title contains mixed characters?

1 Answer

0 votes
by (25.6k points)

You can pass character encoding as an argument to the mb_strlen() function. If you do not pass encoding as an argument, the internal character encoding value will be used. If a string has non-English characters, the length of the string using the strlen() function and the mb_strlen() function will be different. So, you can use these functions to check if the string has non-English characters.

Here is an example:

<?php

    $string = "Thuli visham நடிகர் திலகத்தின் வித்தியாசமான நடிப்பில் என்றும் பார்த்து ரசிக்கும் துளிவிஷம் 4kல்";
    
    if(strlen($string) == mb_strlen($string, 'utf-8')){
      echo "only English characters" . "\n";
    }else{
      echo "non-English characters found" . "\n";
    }
    
?>

...