详解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 相关文章推荐
如何将一个表单同时提交到两个地方处理
Oct 09 PHP
PHP安全配置
Dec 06 PHP
劣质的PHP代码简化
Feb 08 PHP
php学习笔记 面向对象中[接口]与[多态性]的应用
Jun 16 PHP
用PHP实现的四则运算表达式计算实现代码
Aug 02 PHP
PHP正确配置mysql(apache环境)
Aug 28 PHP
浅谈php安全性需要注意的几点事项
Jul 17 PHP
php5.3以后的版本连接sqlserver2000的方法
Jul 28 PHP
Dwz与thinkphp整合下的数据导出到Excel实例
Dec 04 PHP
php中Ctype函数用法详解
Dec 09 PHP
php中JSON的使用方法
Apr 30 PHP
Mac系统下搭建Nginx+php-fpm实例讲解
Dec 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
PHPwind整合最土系统用户同步登录实现方法
2010/12/08 PHP
实例讲解PHP面向对象之多态
2014/08/20 PHP
页面利用渐进式JPEG来提升用户体验度
2014/12/01 PHP
PHP常用的小程序代码段
2015/11/14 PHP
基于PHP实现数据分页显示功能
2016/05/26 PHP
JQuery 表单中textarea字数限制实现代码
2009/12/07 Javascript
javascript Firefox与IE 替换节点的方法
2010/02/24 Javascript
js特效,页面下雪的小例子
2013/06/17 Javascript
解析Jquery取得iframe中元素的几种方法
2013/07/04 Javascript
在子窗口中关闭父窗口的一句代码
2013/10/21 Javascript
JavaScript charCodeAt方法入门实例(用于取得指定位置字符的Unicode编码)
2014/10/17 Javascript
javascript实现控制文字大中小显示
2015/04/28 Javascript
[原创]Javascript 实现广告后加载 可加载百度谷歌联盟广告
2016/05/11 Javascript
Javascript自执行匿名函数(function() { })()的原理浅析
2016/05/15 Javascript
图文详解Javascript中的上下文和作用域
2017/02/15 Javascript
微信小程序之MaterialDesign--input组件详解
2017/02/15 Javascript
Angularjs 实现动态添加控件功能
2017/05/25 Javascript
简单谈谈React中的路由系统
2017/07/25 Javascript
10分钟彻底搞懂Http的强制缓存和协商缓存(小结)
2018/08/30 Javascript
JQuery样式操作、click事件以及索引值-选项卡应用示例
2019/05/14 jQuery
VUE实现Studio管理后台之鼠标拖放改变窗口大小
2020/03/04 Javascript
[00:15]天涯墨客终极技能展示
2018/08/25 DOTA
简单了解python字符串前面加r,u的含义
2019/12/26 Python
Python 使用Opencv实现目标检测与识别的示例代码
2020/09/08 Python
HTML5新增form控件和表单属性实例代码详解
2019/05/15 HTML / CSS
英国网上购买门:Direct Doors
2018/06/07 全球购物
美国领先的眼镜和太阳镜在线零售商:Glasses.com
2019/08/26 全球购物
挂职思想汇报
2013/12/31 职场文书
宾馆总经理岗位职责
2014/02/14 职场文书
党建工作先进材料
2014/05/02 职场文书
教师见习报告范文
2014/11/03 职场文书
高老头读书笔记
2015/06/30 职场文书
为什么阅读对所有年龄段的孩子都很重要?
2019/07/08 职场文书
php字符串倒叙
2021/04/01 PHP
Go各时间字符串使用解析
2021/04/02 Golang
python 下划线的多种应用场景总结
2021/05/12 Python