Search for Arabic letters in Arabic words

Here is my working code:

<!DOCTYPE HTML> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/> </head> <body> <?php $arabic = "ุตุญูŠูุฉ ุงุณุจูˆุนูŠุฉ ู…ุณุชู‚ู„ุฉ ุดุงู…ู„ุฉ ุชุชุงุจุน ุงู„ุงุฎุจุงุฑ ูู‰ ุงู„ู…ู†ุทู‚ุฉ ุงู„ุนุฑุจูŠุฉ"; $french = "que voulez vous dire?"; if (isset($_POST['search'])) { $search = $_POST['search']; $key = $_POST['key']; $td = substr_count($arabic, $key); echo $td; } echo "<br />" . $arabic; function count_occurences($char_string, $haystack, $case_sensitive = true) { if ($case_sensitive === false) { $char_string = strtolower($char_string); $haystack = strtolower($haystack); } $characters = preg_split('//u', $char_string, -1, PREG_SPLIT_NO_EMPTY); //$characters = str_split($char_string); $character_count = 0; foreach ($characters as $character) { $character_count = $character_count + substr_count($haystack, $character); } return $character_count; } ?> <form name="input" action="" method="post"> <input type= "text" name="key" value=""/> <input type ="submit" name="search" value =" find it !"/> </form> </body> </html> 

For $french it works well, but with $arabic it is not. Of course, there is no mistake, but if I enter, for example, ุญ to search for this letter, it always shows 0 for each letter that I entered.

Are there any errors? Or am I missing something with Arabic? I donโ€™t know why in $french works well, if I enter v , it shows the result 2 .

+7
source share
2 answers

You need to use Multibyte string functions .

You can also set mbstring.func_overload = 7 in your php.ini , and php will automatically use multi-byte copies of standard string functions.

See mbstring to overload documentation if you want to use a different value for overloaded functions that would best suit your needs

Also replace

$characters = str_split($char_string);

from

$characters = preg_split('//u', $char_string, -1, PREG_SPLIT_NO_EMPTY);

because str_split not multibyte and has no alternative

Additionally, if no encoding is sent in the headers after the form is submitted, or there are some problems with them, you can install in your php.ini

default_charset = "UTF-8"

+5
source

I checked your UTF-8 encoded code and it works.

i'v added a meta tag:

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
+2
source

All Articles