mb_strpos() - mb函数(多字节字符串转化库)
mb_strpos()
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
查找字符串在另一个字符串中首次出现的位置
说明
mb_strpos(string $haystack,string $needle[,int $offset= 0[,string $encoding= mb_internal_encoding()]]): int查找string在一个string中首次出现的位置。
基于字符数执行一个多字节安全的strpos()操作。第一个字符的位置是 0,第二个字符的位置是 1,以此类推。
参数
$haystack要被检查的string。
$needle在$haystack中查找这个字符串。和strpos()不同的是,数字的值不会被当做字符的顺序值。
$offset搜索位置的偏移。如果没有提供该参数,将会使用 0。负数的 offset 会从字符串尾部开始统计。
$encoding$encoding参数为字符编码。如果省略,则使用内部字符编码。
返回值
返回string的$haystack中$needle首次出现位置的数值。如果没有找到$needle,它将返回FALSE
。
更新日志
版本 | 说明 |
---|---|
7.1.0 | 支持负数的$offset。 |
参见
mb_internal_encoding()
设置/获取内部字符编码strpos()
查找字符串首次出现的位置
It appears that the $offset value is a character count not a byte count. (This may seem obvious but it isn't explicitly stated)
a sample mb_str_replace function: function mb_str_replace($haystack, $search,$replace, $offset=0,$encoding='auto'){ $len_sch=mb_strlen($search,$encoding); $len_rep=mb_strlen($replace,$encoding); while (($offset=mb_strpos($haystack,$search,$offset,$encoding))!==false){ $haystack=mb_substr($haystack,0,$offset,$encoding) .$replace .mb_substr($haystack,$offset+$len_sch,1000,$encoding); $offset=$offset+$len_rep; if ($offset>mb_strlen($haystack,$encoding))break; } return $haystack; }
sorry, my previous post had an error. replace the 1000 with strlen($haystack) to handle strings longer than 1000 chars. btw. This is an issue with the mbstring functions. you can't specify the $encoding without specifying a $length, thus this reduces the functionality of mb_substr compared to substr
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)