详解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判断变量类型常用方法
Apr 24 PHP
判断php数组是否为索引数组的实现方法
Jun 13 PHP
php ZipArchive压缩函数详解实例
Nov 06 PHP
php使用qr生成二维码的示例分享
Jan 20 PHP
PHP使用Alexa API获取网站的Alexa排名例子
Jun 12 PHP
PHP中preg_match函数正则匹配的字符串长度问题
May 27 PHP
初识PHP中的Swoole
Apr 05 PHP
PHP模板引擎Smarty中的保留变量用法分析
Apr 11 PHP
yii通过小物件生成view的方法
Oct 08 PHP
php实现的简单中文验证码功能示例
Jan 03 PHP
PHP封装的多文件上传类实例与用法详解
Feb 07 PHP
php安装dblib扩展,连接mssql的具体步骤
Mar 02 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
PHP+AJAX实现无刷新注册(带用户名实时检测)
2007/01/02 PHP
php中的时间显示
2007/01/18 PHP
php实现的http请求封装示例
2016/11/08 PHP
解决PHP使用CURL发送GET请求时传递参数的问题
2019/10/11 PHP
php设计模式之职责链模式实例分析【星际争霸游戏案例】
2020/03/27 PHP
在IE上直接编辑网页内容的js代码(IE地址栏js)
2009/04/27 Javascript
基于JQuery实现CheckBox全选全不选
2011/06/27 Javascript
jQuery验证Checkbox是否选中的代码 推荐
2011/09/04 Javascript
js的onload事件及初始化按钮事件示例代码
2013/09/25 Javascript
基于JavaScript代码实现pc与手机之间的跳转
2015/12/23 Javascript
浅析Bootstrap缩略图组件与警示框组件
2016/04/29 Javascript
jQuery.cookie.js实现记录最近浏览过的商品功能示例
2017/01/23 Javascript
Vue.js项目部署到服务器的详细步骤
2017/07/17 Javascript
微信小程序上传图片功能(附后端代码)
2020/06/19 Javascript
JS实现带阴历的日历功能详解
2019/01/24 Javascript
Vue使用mixin分发组件的可复用功能
2019/09/01 Javascript
Vue使用Three.js加载glTF模型的方法详解
2020/06/14 Javascript
vue结合el-upload实现腾讯云视频上传功能
2020/07/01 Javascript
vue项目中js-cookie的使用存储token操作
2020/11/13 Javascript
[00:32]2018DOTA2亚洲邀请赛EG出场
2018/04/03 DOTA
Python利用逻辑回归模型解决MNIST手写数字识别问题详解
2020/01/14 Python
关于Kotlin中SAM转换的那些事
2020/09/15 Python
HTML5地理定位实例
2014/10/15 HTML / CSS
英国领先的办公用品供应商:Viking
2016/08/01 全球购物
Under Armour美国官网:美国知名高端功能性运动品牌
2016/09/05 全球购物
描述Cookie和Session的作用,区别和各自的应用范围,Session工作原理
2015/03/25 面试题
党员培训思想汇报
2014/01/07 职场文书
中医临床专业自我鉴定范文
2014/01/15 职场文书
八年级生物教学反思
2014/01/22 职场文书
财务出纳岗位职责
2014/02/03 职场文书
保护环境建议书400字
2014/05/13 职场文书
小学班主任自我评价
2015/03/11 职场文书
党性教育心得体会(共6篇)
2016/01/21 职场文书
导游词之山东八大关
2019/12/18 职场文书
MySQL8.0的WITH查询详情
2021/08/30 MySQL
python图像处理 PIL Image操作实例
2022/04/09 Python