详解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通用检测函数集合
Nov 25 PHP
PHP生成Flash动画的实现代码
Mar 12 PHP
php强制下载类型的实现代码
Apr 21 PHP
微博短链接算法php版本实现代码
Sep 15 PHP
解决cPanel无法安装php5.2.17
Jun 22 PHP
四个常见html网页乱码问题及解决办法
Sep 08 PHP
PHP中的Trait 特性及作用
Apr 03 PHP
php pdo oracle中文乱码的快速解决方法
May 16 PHP
PHP flush 函数使用注意事项
Aug 26 PHP
php提交表单时保留多个空格及换行的文本样式的方法
Jun 20 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
Jul 20 PHP
php框架CI(codeigniter)自动加载与自主创建对象操作实例分析
Jun 06 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
Win9x/ME下Apache+PHP安装配置
2006/10/09 PHP
php中如何判断一个网页请求是ajax请求还是普通请求
2013/08/10 PHP
PHP APC缓存配置、使用详解
2014/03/06 PHP
thinkphp3.x中session方法的用法分析
2016/05/20 PHP
PHP+jQuery实现滚屏无刷新动态加载数据功能详解
2017/05/04 PHP
php中array_fill函数的实例用法
2021/03/02 PHP
IE 下的只读 innerHTML
2009/08/21 Javascript
ExtJS Window 最小化的一种方法
2009/11/18 Javascript
JS实现淘宝幻灯片效果的实现方法
2013/03/22 Javascript
javascript-表格排序(降序/反序)实现介绍(附图)
2013/05/30 Javascript
angularJS 中$scope方法使用指南
2015/02/09 Javascript
JavaScript设置、获取、清除单值和多值cookie的方法
2015/11/17 Javascript
提高Web性能的前端优化技巧总结
2017/02/27 Javascript
解决angularjs service中依赖注入$scope报错的问题
2018/10/02 Javascript
微信小程序发送短信验证码完整实例
2019/01/07 Javascript
vue-video-player实现实时视频播放方式(监控设备-rtmp流)
2020/08/10 Javascript
vue 使用饿了么UI仿写teambition的筛选功能
2021/03/01 Vue.js
[57:31]DOTA2-DPC中国联赛 正赛 SAG vs CDEC BO3 第一场 2月1日
2021/03/11 DOTA
Python MD5文件生成码
2009/01/12 Python
微信 用脚本查看是否被微信好友删除
2016/10/28 Python
PyTorch快速搭建神经网络及其保存提取方法详解
2018/04/28 Python
Python查找文件中包含中文的行方法
2018/12/19 Python
Python 使用matplotlib模块模拟掷骰子
2019/08/08 Python
Django框架反向解析操作详解
2019/11/28 Python
Django数据结果集序列化并展示实现过程
2020/04/22 Python
python获取linux系统信息的三种方法
2020/10/14 Python
护理专科毕业推荐信
2013/11/10 职场文书
重阳节登山活动方案
2014/02/03 职场文书
岗位竞聘演讲稿范文
2014/04/24 职场文书
难忘的一天教学反思
2014/04/30 职场文书
安全责任书模板
2014/07/22 职场文书
学生自我评语
2015/01/04 职场文书
领导欢迎词范文
2015/01/26 职场文书
2016新年年会主持词
2015/07/06 职场文书
2016大学生暑期三下乡心得体会
2016/01/23 职场文书
Win11显卡控制面板打开显卡设置方法
2022/04/20 数码科技