PHP之mb_strpos使用
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是世界上最好的语言
PHP之mb_strpos使用的更多相关文章
- php 基础代码大全(不断完善中)
下面是基础的PHP的代码,不断完善中~ //语法错误(syntax error)在语法分析阶段,源代码并未被执行,故不会有任何输出. /* [命名规则] */ 常量名 类常量建议全大写,单词间用下划线 ...
- 常用function() 收集
1.随机数生成函数(来源-微信支付demo案例) /** * * 产生随机字符串,不长于32位 * @param int $length * @return 产生的随机字符串 */ public st ...
- PHP7函数大全(4553个函数)
转载来自: http://www.infocool.net/kb/PHP/201607/168683.html a 函数 说明 abs 绝对值 acos 反余弦 acosh 反双曲余弦 addcsla ...
- php-设置关键词高亮的字符串处理函数
/** * 设置关键词高亮的字符串处理函数 * @param [string] $str [要高亮的字符串] * @param array $word_arr [关键词] */function set ...
- PHP字符串处理
/*1 字符串格式化 */ $str = ' php '; //清理两边的空格trim();,左边ltrim(); 边rtrin() echo trim($str); //nl2br();将换行符\n ...
- 两千行PHP学习笔记
亲们,如约而至的PHP笔记来啦~绝对干货! 以下为我以前学PHP时做的笔记,时不时的也会添加一些基础知识点进去,有时还翻出来查查. MySQL笔记:一千行MySQL学习笔记http://www.cnb ...
- PHP用mb_string函数库处理与windows相关中文字符
昨天想批处理以前下载的一堆文件,把文件里的关键内容用正则匹配出来,集中处理.在操作文件时遇到一个问题,就是windows操作系统中的编码问题. 我们都知道windows中(当然是中文版),文件名和文件 ...
- PHP配置详解
[PHP] ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;; ; This file controls many aspects of ...
- 如何优雅的使用 phpStorm 开发工具
按照惯例依然是从百科上复制一条简介: PhpStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具.PhpStorm可随时帮助用户对其编码进行调整,运行单元测试或者提供可视化d ...
随机推荐
- 咏南中间件修正了一处BUG,调用中间件插件会报:非法访问
咏南中间件修正了一处BUG,调用中间件插件会报:非法访问将以下方法修改成如下的代码即可function TServerMethods1.GetSvrData(const accountNo, defi ...
- day14(带参装饰器,迭代器,生成器,枚举对象)
一,复习 ''' 函数的嵌套定义:在函数内部定义另一个函数 闭包:被嵌套的函数 -- 1.外层通过形参给内层函数传参 -- 2.验证执行 开放封闭原则: 功能可以拓展,但源代码与调用方式都不可以改变 ...
- cron和crontab
crontab -l 列出目前的计划任务(时程表) crontab -e 编辑计划任务 计划任务的格式如下: f1 f2 f3 f4 f5 program 其中 f1 是表示分钟,f2 表示小时, ...
- C# superGridControl 样式设置、加载数据、获取数据
样式设置 superGridControl1.PrimaryGrid.SelectionGranularity = SelectionGranularity.Cell; //设置选中样式 单元格.整列 ...
- ceph 存储池PG查看和PG存放OSD位置
1. 查看PG (ceph-mon)[root@controller /]# ceph pg stat 512 pgs: 512 active+clean; 0 bytes data, 1936 MB ...
- ceph: health_warn clock skew detected on mon的解决办法
造成集群状态health_warn:clock skew detected on mon节点的原因有两个,一个是mon节点上ntp服务器未启动,另一个是ceph设置的mon的时间偏差阈值比较小. 排查 ...
- JavaWeb -pageContext/request/session/application
pageContext/request/session/application总结 一.范围差异 1. pageContext jsp页面容器 当前页面有效 2. request 请求对象 同一次请求 ...
- opencv3.3 安装环境教程以及实现个图片读取功能
一.opencv3.3安装环境 1. 首先要安装这个opencv3.3,我们必须要安装一个python (IDLE:集成开发环境),我自己安装了IDLE3.6和pycharm2017,这些软件都可以去 ...
- “全栈2019”Java异常第二章:如何处理异常?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- Django-DRF-视图的演变
版本一(基于类视图APIView类) views.py: APIView是继承的Django View视图的. from .serializers import UserSerializers #导入 ...