2

I need help... I have hebrew php string and I want search position of substring. my code:

$string = "אבגד הוזח טי";
$find = "הוזח";
$pos = strpos($string, $find);
echo $pos;

The strpos found the substring, but return wrong value of position. It return $pos value 9 Instead of 5. Why the strpos not working in hebrew strings? Can you help me please?

igorb0214
  • 67
  • 2
  • 2
  • 9

2 Answers2

3

Hebrew strings use multibyte characters, so each "character" can be 2 or more characters long, and not 1, like most latin-based characters. You will probably want to look into PHP Multibyte String Functions for your application.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
  • This isn't actually true. It might be a very different encoding as well. Both UTF-8 and UTF-16 support Hebrew characters. They differ in how they are stored however. This all highly depends on what encoding the file was stored as, and what encoding PHP is set to use by default (usually via the locale). The suggestion to use PHP multibyte aware functions however is very much spot-on. – Tularis Apr 10 '14 at 00:35
  • Thanks @Tularis, I've clarified my answer. – Sunny Patel Apr 10 '14 at 00:37
3

Try using mb_strpos. You will have to set your internal character encoding to UTF-8 using mb_internal_encoding.

mb_internal_encoding("UTF-8");
$string = "אבגד הוזח טי";
$find = "הוזח";
$pos = mb_strpos($string, $find);
echo $pos; //5
tchow002
  • 91
  • 5