详解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
dedecms系统常用术语汇总
Apr 03 PHP
php中对xml读取的相关函数的介绍一
Jun 05 PHP
php数组函数序列之array_flip() 将数组键名与值对调
Nov 07 PHP
基于PHP输出缓存(output_buffering)的深入理解
Jun 13 PHP
PHP获取当前完整URL地址的函数
Dec 21 PHP
PHP中文编码小技巧
Dec 25 PHP
smarty模板引擎之分配数据类型
Mar 30 PHP
PHP观察者模式原理与简单实现方法示例
Aug 25 PHP
php使用curl下载指定大小的文件实例代码
Sep 30 PHP
php使用curl伪造来源ip和refer的方法示例
May 08 PHP
lnmp安装多版本PHP共存的方法详解
Aug 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绘图中显示不出图片的原因及解决
2014/03/05 PHP
Symfony2学习笔记之系统路由详解
2016/03/17 PHP
Yii2框架dropDownList下拉菜单用法实例分析
2016/07/18 PHP
利用PHP判断文件是否为图片的方法总结
2017/01/06 PHP
jquery 简单导航实现代码
2009/09/11 Javascript
javascript中创建对象的三种常用方法
2010/12/30 Javascript
在jquery中处理带有命名空间的XML数据
2011/06/13 Javascript
Web Uploader文件上传插件使用详解
2016/05/10 Javascript
谈谈jQuery之Deferred源码剖析
2016/12/19 Javascript
在javascript中,null>=0 为真,null==0却为假,null的值详解
2017/02/22 Javascript
图解Javascript——作用域、作用域链、闭包
2017/03/21 Javascript
详解webpack-dev-server的简单使用
2018/04/02 Javascript
vue实现组件之间传值功能示例
2018/07/13 Javascript
Vue 后台管理类项目兼容IE9+的方法示例
2019/02/20 Javascript
angularjs1.X 重构controller 的方法小结
2019/08/15 Javascript
[04:44]DOTA2 2017全国高校联赛视频回顾
2017/08/21 DOTA
Django中处理出错页面的方法
2015/07/15 Python
深入理解python try异常处理机制
2016/06/01 Python
Python中用post、get方式提交数据的方法示例
2017/09/22 Python
python爬虫 2019中国好声音评论爬取过程解析
2019/08/26 Python
Python3如何判断三角形的类型
2020/04/12 Python
python3+selenium获取页面加载的所有静态资源文件链接操作
2020/05/04 Python
Python ADF 单位根检验 如何查看结果的实现
2020/06/03 Python
HTML5的革新 结构之美
2011/06/20 HTML / CSS
html5本地存储之localstorage 、本地数据库、sessionStorage简单使用示例
2014/05/08 HTML / CSS
印度购物网站:TATA CLiQ
2017/11/23 全球购物
英国最大的户外商店:Go Outdoors
2019/04/17 全球购物
班组安全员工作职责
2014/02/01 职场文书
反腐倡廉剖析材料
2014/09/30 职场文书
四风问题查摆剖析材料
2014/10/11 职场文书
因身体原因离职的辞职信范文
2015/05/12 职场文书
2016庆祝国庆67周年宣传语
2015/11/25 职场文书
三严三实·严以修身心得体会
2016/01/15 职场文书
2016年幼儿园庆六一开幕词
2016/03/04 职场文书
写给消防战士们的一封慰问信
2019/10/07 职场文书
基于Python实现nc批量转tif格式
2022/08/14 Python