strpos return wrong position at hebrew

半城伤御伤魂 提交于 2020-01-25 04:26:27

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/22976410/strpos-return-wrong-position-at-hebrew

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!