详解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中数组的三种排序方法分享
May 07 PHP
spl_autoload_register与autoload的区别详解
Jun 03 PHP
解决PHP超大文件下载,断点续传下载的方法详解
Jun 06 PHP
手把手教你打印出PDF(关于fpdf的简单应用)
Jun 25 PHP
php中json_encode处理gbk与gb2312中文乱码问题的解决方法
Jul 10 PHP
深入浅析php json 格式控制
Dec 24 PHP
PHP中模拟链表和链表的基本操作示例
Feb 27 PHP
php+ajax简单实现全选删除的方法
Dec 06 PHP
php登录超时检测功能实例详解
Mar 21 PHP
PHP实现批量重命名某个文件夹下所有文件的方法
Sep 04 PHP
yii框架数据库关联查询操作示例
Oct 14 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
May 15 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
解决文件名解压后乱码的问题 将文件名进行转码的代码
2012/01/10 PHP
PHP登录环节防止sql注入的方法浅析
2014/06/30 PHP
ThinkPHP中pathinfo的访问模式、路径访问模式及URL重写总结
2014/08/23 PHP
PHP 芝麻信用接入的注意事项
2016/12/01 PHP
PHP中mysqli_get_server_version()的实例用法
2020/02/03 PHP
jquery 图片预加载 自动等比例缩放插件
2008/12/25 Javascript
用JSON做数据传输格式中的一些问题总结
2011/12/21 Javascript
JavaScript中的toLocaleDateString()方法使用简介
2015/06/12 Javascript
基于JQuery实现仿网易邮箱全屏动感滚动插件fullPage
2015/09/20 Javascript
JavaScript实现自动生成网页元素功能(按钮、文本等)
2015/11/21 Javascript
日常收集整理的JavaScript常用函数方法
2015/12/10 Javascript
iScroll.js 使用方法参考
2016/05/16 Javascript
Javascript中prototype的使用详解
2016/06/18 Javascript
js 弹出对话框(遮罩)透明,可拖动的简单实例
2016/07/11 Javascript
浅谈jQuery中的eq()与DOM中element.[]的区别
2016/10/28 Javascript
bootstrap-datetimepicker实现只显示到日期的方法
2016/11/25 Javascript
详解jQuery简单的表单应用
2016/12/16 Javascript
JavaScript 获取元素在父节点中的下标(推荐)
2017/06/28 Javascript
JavaScript中EventLoop介绍
2018/01/22 Javascript
微信小程序实现天气预报功能
2018/07/18 Javascript
JQuery扩展对象方法操作示例
2018/08/21 jQuery
vue elementui 实现搜索栏公共组件封装的实例代码
2020/01/20 Javascript
js校验开始时间和结束时间
2020/05/26 Javascript
Python3 JSON编码解码方法详解
2019/09/06 Python
Ubuntu下Python+Flask分分钟搭建自己的服务器教程
2019/11/19 Python
matplotlib 对坐标的控制,加图例注释的操作
2020/04/17 Python
python 实现两个npy档案合并
2020/07/01 Python
Skyscanner阿联酋:全球领先的旅游搜索平台
2017/11/25 全球购物
中东地区为妈妈们提供一切的头号购物目的地:Sprii
2018/05/06 全球购物
中科软笔试题和面试题
2014/10/07 面试题
管理科学大学生求职信
2013/11/13 职场文书
教师自荐信范文
2013/12/09 职场文书
党员干部三严三实心得体会
2014/10/13 职场文书
2015年销售工作总结范文
2015/03/30 职场文书
运动会加油稿30字
2015/07/21 职场文书
详解Vue项目的打包方式(生成dist文件)
2022/01/18 Vue.js