文字

mb_strpos

(PHP 4 >= 4.0.6, PHP 5)

mb_strpos查找字符串在另一个字符串中首次出现的位置

说明

int mb_strpos ( string $haystack , string $needle [, int $offset = 0 [, string $encoding = mb_internal_encoding() ]] )

查找 string 在一个 string 中首次出现的位置。

基于字符数执行一个多字节安全的 strpos() 操作。 第一个字符的位置是 0,第二个字符的位置是 1,以此类推。

参数

haystack

要被检查的 string

needle

haystack 中查找这个字符串。 和 strpos() 不同的是,数字的值不会被当做字符的顺序值。

offset

搜索位置的偏移。如果没有提供该参数,将会使用 0。

encoding

encoding 参数为字符编码。如果省略,则使用内部字符编码。

返回值

返回 string haystackneedle 首次出现位置的数值。 如果没有找到 needle,它将返回 FALSE

参见

  • mb_internal_encoding() - 设置/获取内部字符编码
  • strpos() - 查找字符串首次出现的位置

用户评论:

[#1] stestagg at talk21 dot com [2006-08-05 17:12:41]

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

[#2] stestagg at talk21 dot com [2006-08-04 09:42:21]

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;
}

[#3] stestagg at talk21 dot com [2006-08-04 09:39:43]

It appears that the $offset value is a character count not a byte count.  (This may seem obvious but it isn't explicitly stated)

上一篇: 下一篇: