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 ...
随机推荐
- 咏南CS开发框架新的界面风格
咏南CS开发框架新的界面风格 这种风格完全和WINDOWS桌面一样,符合用户的操作习惯. 我的桌面的图标和WINDOWS桌面一样分为二类:1)快捷方式 2)文件夹. 如果快捷方式较多,看得人眼花缭乱 ...
- 【转】不用软件,解压Win8/Win8.1的install.wim文件
今天用好压解压Windows 8.1的install.wim文件,居然提示文件损坏,换了7Z仍然如此:其实文件是好的.只不过这些软件暂时不支持罢了,还好可以用dism命令来手动完成. 一.检查镜像版本 ...
- 使用Jenkins容器构建时,关于maven项目settings.xml的位置
切记切记:必须要放在 /root/.m2/settings.xml,要不然Jenkins容器中执行mvn命令时settings会不生效
- TypeScript白鹭引擎Egret防止按钮事件冒泡穿透
this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (event) => { if (event.target!=this) return;/ ...
- C# SQLite 数据库
数据库 Oracle.Oracle的应用,主要在传统行业的数据化业务中,比如:银行.金融这样的对可用性.健壮性.安全性.实时性要求极高的业务 MS SQL Server.windows生态系统的产品, ...
- WPF 降低.net framework到4.0
1. 问题背景 由于xp系统上面最高只能安装.net framework 4.0,所以公司项目需要将原来项目的.net framework版本降低到4.0,具体的降版本很简单,只要把项目属性中的目标框 ...
- This InfoPath form template is browser-compatible, but it cannot be browser-enabled on the selected site
- all features were running on sitecollection level and at site level But here is the solution, i do ...
- Android Bug BaseExpandableListAdapter, getChildView
@Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastC ...
- linux 使用内存作为 /tmp 临时文件夹
方法一:cat /etc/fstabtmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 方法二:mount tmpfs /tmp -t tmpfs -o s ...
- JavaWeb -jsp文件和内置对象的解析
jsp文件和内置对象的解析 对page解析 JSP九大内置对象(自带,无需new) 1 out:输出对象 2 request:请求对象,存储“客户端像服务端发送的请求信息” 3 response:响应 ...