Question

I have a arabic keyword in a mysql table like

    *#1591; *#1610; *#1585;*#1575;*#1606

// Please consider & in the place of * , value with '&' automatically converts in to arabic.

Mysql table encoding: utf8_general_ci

I am getting some string from the external resources example twitter.

I would like to match the keyword with the tweet i am getting .

 $tweet = 'وينج وأداسي الاماراتية توقعان اتفاقية تعاون لتوفير أنظمة الطائرات بدون طيا';

  $keyword = '*#1591; *#1610; *#1585;*#1575;*#1606'; //From db

  $status = strpos ($tweet, $keyword)

$status always returns false.

I have checked with utf8_encode(), utf_8_decode() , mb_strpos() without any luck.

I know need to convert both strings to one common format before compare but which format i need to convert ?

Please help me on this.

Was it helpful?

Solution

As arabic symbols are encoded using multibyte characters, you must use functions that support such a constraint: grapheme_strpos and mb_strpos (in that order).

Using them instead of plain old strpos will do the trick.

Also, keep in mind that you may have to check for its availability prior to its use, as not all hosted environments have them enabled:

if (function_exists('grapheme_strpos')) {
    $pos = grapheme_strpos($tweet, $keyword);
} elseif (function_exists('mb_strpos')) {
    $pos = mb_strpos($tweet, $keyword);
} else {
    $pos = strpos($tweet, $keyword);
}

And last but not least, check the docs for the different arguments that functions take, as the encoding used by the strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top