Take the 2-minute tour ×

here ny working code



 $arabic = "صحيفة اسبوعية مستقلة شاملة تتابع الاخبار فى المنطقة العربية";
 $french = "que voulez vous dire?";

$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;
<input  type= "text" name="key" value=""/>
<input  type ="submit" name="search" value =" find it !"/>


for the $french it works good , however with $arabic no. of course there is no error but if i enter forexample ح to search for that letter it shows always 0 forevery letter i enter.

is there some wrong ? or im missing something with arabic. i dont know why in $french works good if i enter v it shows 2 in result.

any help please ?

share|improve this question
maybe encoding it with uf8_encode and adding the charset? check if this answer helps stackoverflow.com/questions/2309496/php-print-arabic-string –  Youn Elan Mar 30 '13 at 13:31
add comment

2 Answers

up vote 5 down vote accepted

You need to use Multibyte String Functions.

You can also set mbstring.func_overload = 7 in your php.ini, and php will automatically use multibyte counterparts for standard string functions.

Look at mbstring overloading documentation if you want to use some other value for overloaded functions which would suit your needs better

Also, replace

$characters = str_split($char_string);



because str_split is not multibyte safe and has no alternative

Additionaly, if no encoding is sent in the headers after you submit the form, or there is some issue with them, you can set in your php.ini

default_charset = "UTF-8"

share|improve this answer
It works fine :) Thanks ! –  echo_Me Mar 30 '13 at 15:46
add comment

i tested your code with Encoding UTF-8, and it's work..

i'v added a meta tag:

share|improve this answer
sorry i have meta tag , i updated my answer –  echo_Me Mar 30 '13 at 14:05
so what's your file encoding? is utf-8? –  mehdi Mar 30 '13 at 14:20
yes everything is ut-8 , file , editor , ... –  echo_Me Mar 30 '13 at 14:26
add comment

Your Answer


