详解PHP中mb_strpos的使用


Posted in PHP onFebruary 04, 2018

mb_strpos

(PHP 4 >= 4.0.6, PHP 5, PHP 7)
mb_strpos — Find position of first occurrence of string in a string
mb_strpos — 查找字符串在另一个字符串中首次出现的位置

Description

int mb_strpos ( 
  string $haystack , 
  string $needle [, 
  int $offset = 0 [, 
  string $encoding = mb_internal_encoding() ]] 
  )
//Finds position of the first occurrence of a string in a string.
// 查找 string 在一个 string 中首次出现的位置。

//Performs a multi-byte safe strpos() operation based on number of characters. The first character's position is 0, the second character position is 1, and so on.
// 基于字符数执行一个多字节安全的 strpos() 操作。 第一个字符的位置是 0,第二个字符的位置是 1,以此类推。

Parameters

haystack

  • The string being checked.
  • 要被检查的 string。

needle

  • The string to find in haystack. In contrast with strpos(), numeric values are not applied as the ordinal value of a character.
  • 在 haystack 中查找这个字符串。 和 strpos() 不同的是,数字的值不会被当做字符的顺序值。

offset

  • The search offset. If it is not specified, 0 is used. A negative offset counts from the end of the string.
  • 搜索位置的偏移。如果没有提供该参数,将会使用 0。负数的 offset 会从字符串尾部开始统计。

encoding

  • The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used.
  • encoding 参数为字符编码。如果省略,则使用内部字符编码。

Return Values

  • Returns the numeric position of the first occurrence of needle in the haystack string. If needle is not found, it returns FALSE.
  • 返回 string 的 haystack 中 needle 首次出现位置的数值。 如果没有找到 needle,它将返回 FALSE。

Example

<?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/2/2
 * Time: 下午11:16
 */

$str = "Hello World! Hello PHP";
$pos = mb_strpos( $str, "Hello", 0, mb_internal_encoding() );
echo $pos . PHP_EOL;//0
$pos = mb_strpos( $str, "Hello", 2, mb_internal_encoding() );
echo $pos . PHP_EOL;//13

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,
        $le = mb_strlen( $haystack ) - mb_strlen( $search ) + mb_strlen( $replace ),
        $encoding );
    //echo $le.PHP_EOL;
    $offset = $offset + $len_rep;
    if ( $offset > mb_strlen( $haystack, $encoding ) ) {
      break;
    }
  }
  
  return $haystack;
}

$replace = mb_str_replace( "hello world !hello world !hello world !hello world !", "hello", "hi" );
echo $replace . PHP_EOL; //hi world !hi world !hi world !hi world !

//hi PHP !hi PHP !hi PHP !hi PHP !
echo mb_str_replace( $replace, "world", "PHP" ) . PHP_EOL;
echo mb_str_replace( $replace, " ", "-" ) . PHP_EOL;

//PHP是世界上最好的语言??????
echo mb_str_replace( "PHP是世界上最好的语言??????", '?', '?', 0, mb_internal_encoding() ) . PHP_EOL;
echo mb_str_replace( "112233445566", '22', '00' ) . PHP_EOL;//110033445566
echo mb_str_replace( '????', '?', '?1', 2, mb_internal_encoding() ) . PHP_EOL;
echo mb_str_replace( '1111', '111', '0', 1 ) . PHP_EOL;//10
echo mb_strlen( '????' ) . PHP_EOL;//4

//代码开发代码
echo mb_str_replace( '软件开发软件', '软件', '代码' ,0,mb_internal_encoding()) . PHP_EOL;
//代码开发 //todo??
echo mb_str_replace( '软件开发软件', '软件', '代码' ) . PHP_EOL;
PHP 相关文章推荐
用php实现像JSP,ASP里Application那样的全局变量
Jan 12 PHP
PHP下常用正则表达式整理
Oct 26 PHP
无需重新编译php加入ftp扩展的解决方法
Feb 07 PHP
PHP 利用Mail_MimeDecode类提取邮件信息示例
Jan 26 PHP
Laravel 4.2 中队列服务(queue)使用感受
Oct 30 PHP
thinkphp3.2.2实现生成多张缩略图的方法
Dec 19 PHP
CentOS6.5 编译安装lnmp环境
Dec 21 PHP
PHP中创建和验证哈希的简单方法实探
Jul 06 PHP
php使用strip_tags()去除html标签仍有空白的解决方法
Jul 28 PHP
PHP 表单提交及处理表单数据详解及实例
Dec 27 PHP
详解PHP发送邮件知识点
May 06 PHP
PHP实现通过CURL上传文件功能示例
May 30 PHP
详解PHP文件的自动加载(autoloading)
Feb 04 #PHP
PHP实现QQ登录的开原理和实现过程
Feb 04 #PHP
PHP实现正则表达式分组捕获操作示例
Feb 03 #PHP
php实现解析xml并生成sql语句的方法
Feb 03 #PHP
PHP删除数组中指定下标的元素方法
Feb 03 #PHP
php学习笔记之mb_strstr的基本使用
Feb 03 #PHP
php通过pecl方式安装扩展的实例讲解
Feb 02 #PHP
You might like
图解上海144收音机
2021/03/02 无线电
使用Xdebug调试和优化PHP程序之[1]
2007/04/17 PHP
PHP中4个加速、缓存扩展的区别和选用建议
2014/03/12 PHP
PHP函数实现从一个文本字符串中提取关键字的方法
2015/07/01 PHP
javascript 拖放效果实现代码
2010/01/22 Javascript
JavaScript 类型的包装对象(Typed Wrappers)
2011/10/27 Javascript
JavaScript动态加载样式表的方法
2015/03/21 Javascript
举例讲解Node.js中的Writable对象
2015/07/29 Javascript
jQuery实现仿美橙互联两级导航菜单效果完整实例
2015/09/17 Javascript
Jquery操作cookie记住用户名
2016/03/29 Javascript
JavaScript绑定事件监听函数的通用方法
2016/05/14 Javascript
基于JavaScript实现五子棋游戏
2020/08/26 Javascript
JS实现导出Excel的五种方法详解【附源码下载】
2018/03/15 Javascript
javascript实现移动端红包雨页面
2020/06/23 Javascript
小程序自定义圆形进度条
2020/11/17 Javascript
python 示例分享---逻辑推理编程解决八皇后
2014/07/20 Python
python使用opencv读取图片的实例
2017/08/17 Python
python如何给字典的键对应的值为字典项的字典赋值
2019/07/05 Python
python 监控服务器是否有人远程登录(详细思路+代码)
2020/12/18 Python
纯CSS3制作漂亮带动画效果的主机价格表
2015/04/25 HTML / CSS
墨尔本复古时尚品牌:Dangerfield
2018/12/12 全球购物
心理健康课教学反思
2014/02/13 职场文书
揭牌仪式主持词
2014/03/19 职场文书
工作粗心大意检讨书
2014/09/18 职场文书
2014市国税局对照检查材料思想汇报
2014/09/23 职场文书
2014年平安创建工作总结
2014/11/24 职场文书
2014年保育员个人工作总结
2014/12/02 职场文书
2014年机关后勤工作总结
2014/12/16 职场文书
企业爱心捐款倡议书
2015/04/27 职场文书
机关干部作风整顿心得体会
2016/01/22 职场文书
2019生态环境保护倡议书!
2019/07/03 职场文书
假如给我三天光明:舟逆水而行,人遇挫而达 
2019/10/29 职场文书
解决SpringBoot跨域的三种方式
2021/06/26 Java/Android
golang用type-switch判断interface的实际存储类型
2022/04/14 Golang
git中cherry-pick命令的使用教程
2022/06/25 Servers
postgresql如何找到表中重复数据的行并删除
2023/05/08 MySQL