详解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 相关文章推荐
DOMXML函数笔记
Oct 09 PHP
用php获取远程图片并把它保存到本地的代码
Apr 07 PHP
PHP开发者常犯的10个MySQL错误更正剖析
Jan 30 PHP
phpmyadmin打开很慢的解决方法
Apr 21 PHP
基于laravel制作APP接口(API)
Mar 15 PHP
Yii2使用swiftmailer发送邮件的方法
May 03 PHP
PHP获取网站中各文章的第一张图片的代码示例
May 20 PHP
用php和jQuery来实现“顶”和“踩”的投票功能
Oct 13 PHP
PHP封装返回Ajax字符串和JSON数组的方法
Feb 17 PHP
form自动提交实例讲解
Jul 10 PHP
PHP延迟静态绑定的深入讲解
Apr 02 PHP
laravel5.6 框架操作数据 Eloquent ORM用法示例
Jan 26 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
PHP4之COOKIE支持详解
2006/10/09 PHP
PHP入门
2006/10/09 PHP
PHP常用的文件操作函数经典收藏
2013/04/02 PHP
thinkphp实现图片上传功能分享
2014/03/04 PHP
Codeigniter注册登录代码示例
2014/06/12 PHP
PHP正则替换函数preg_replace和preg_replace_callback使用总结
2014/09/22 PHP
php目录拷贝实现方法
2015/07/10 PHP
通过JS 获取Mouse Position(鼠标坐标)的代码
2009/09/21 Javascript
javascript中substr,substring,slice.splice的区别说明
2010/11/25 Javascript
js中eval详解
2012/03/30 Javascript
JS中的hasOwnProperty()和isPrototypeOf()属性实例详解
2016/08/11 Javascript
jQuery Form插件使用详解_动力节点Java学院整理
2017/07/17 jQuery
js使用原型对象(prototype)需要注意的地方
2017/08/28 Javascript
微信小程序入门之广告条实现方法示例
2018/12/05 Javascript
Python中time模块与datetime模块在使用中的不同之处
2015/11/24 Python
Python实现自动添加脚本头信息的示例代码
2016/09/02 Python
python利用不到一百行代码实现一个小siri
2017/03/02 Python
利用pandas读取中文数据集的方法
2018/07/25 Python
Python 调用 Outlook 发送邮件过程解析
2019/08/08 Python
python turtle工具绘制四叶草的实例分享
2020/02/14 Python
Python栈的实现方法示例【列表、单链表】
2020/02/22 Python
python中round函数如何使用
2020/06/19 Python
Python urlopen()参数代码示例解析
2020/12/10 Python
html5文本内容_动力节点Java学院整理
2017/07/11 HTML / CSS
html5 canvas合成海报所遇问题及解决方案总结
2017/08/03 HTML / CSS
100%羊绒:NakedCashmere
2020/08/26 全球购物
中科软测试工程师面试题
2012/06/16 面试题
高一政治教学反思
2014/01/28 职场文书
税务会计岗位职责
2014/02/18 职场文书
2014年五四青年节演讲稿范文
2014/04/22 职场文书
新学期国旗下演讲稿
2014/05/08 职场文书
小学美术兴趣小组活动总结
2014/07/07 职场文书
教育专业毕业生推荐信
2014/07/10 职场文书
私人房屋买卖协议书
2014/10/04 职场文书
新党员入党决心书
2015/09/22 职场文书
Go 语言下基于Redis分布式锁的实现方式
2021/06/28 Golang