mb_substr

  • (PHP 4 >= 4.0.6, PHP 5, PHP 7)
  • mb_substr — Get part of string
  • mb_substr — 获取部分字符串

Description

string mb_substr (
string $str ,
int $start [,
int $length = NULL [,
string $encoding = mb_internal_encoding() ]]
)
// Performs a multi-byte safe substr() operation based on number of characters. Position is counted from
// the beginning of str. First character's position is 0. Second character position is 1, and so on.
//根据字符数执行一个多字节安全的 substr() 操作。 位置是从 str 的开始位置进行计数。 第一个字符的位置是 0。第二个字符的位置是 1,以此类推。

Parameters

str

  • The string to extract the substring from.
  • 从该 string 中提取子字符串。

start

  • If start is non-negative, the returned string will start at the start'th position in str, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

  • 如果 start 不是负数,返回的字符串会从 str 第 start 的位置开始,从 0 开始计数。举个例子,字符串 'abcdef',位置 0 的字符是 'a',位置 2 的字符是 'c',以此类推。

  • If start is negative, the returned string will start at the start'th character from the end of str.

  • 如果 start 是负数,返回的字符串是从 str 末尾处第 start 个字符开始的。

length

  • Maximum number of characters to use from str. If omitted or NULL is passed, extract all characters to the end of the string.
  • str 中要使用的最大字符数。如果省略了此参数或者传入了 NULL,则会提取到字符串的尾部。

encoding

  • The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used.
  • encoding 参数为字符编码。如果省略,则使用内部字符编码。

Return Values

  • mb_substr() returns the portion of str specified by the start and length parameters.
  • mb_substr() 函数根据 start 和 length 参数返回 str 中指定的部分。

Changelog

  • 5.4.8 - Passing NULL as length extracts all characters to the end of the string. Prior to this version NULL was treated the same as 0.

Examples

<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/1/30
* Time: 下午8:51
*/ $string = "0123456789你好";
/** start > 0 length > 0*/
$mystring = mb_substr( $string, 5, 1 );
echo $mystring . PHP_EOL; // 5
$mystring = mb_substr( $string, 5, 2 );
echo $mystring . PHP_EOL; // 56
$mystring = mb_substr( $string, 10, 2 );
echo $mystring . PHP_EOL; // 你好 /** start < 0 length > 0*/
$mystring = mb_substr( $string, - 2, 2 );
echo $mystring . PHP_EOL; // 你好
echo 'mb_strlen : ' . mb_strlen( $string ) . PHP_EOL;//12
$mystring = mb_substr( $string, - mb_strlen( $string ), 2 );
echo $mystring . PHP_EOL; // 01
$mystring = mb_substr( $string, - 3, 2 );
echo $mystring . PHP_EOL; // 9你 /** start > 0 length < 0*/
$mystring = mb_substr( $string, 5, - 1 );
echo $mystring . PHP_EOL; // 56789你
$mystring = mb_substr( $string, 0, - mb_strlen( $string ) + 1 );
echo $mystring . PHP_EOL; // 0
$mystring = mb_substr( $string, 5, - 5 );
echo $mystring . PHP_EOL; // 56 /** start < 0 length < 0*/
$mystring = mb_substr( $string, - 10, - 1 );
echo $mystring . PHP_EOL; // 23456789你
$mystring = mb_substr( $string, - 5, - 1 );
echo $mystring . PHP_EOL; // 789你 function mb_ucfirst( $str, $enc = 'utf-8' ) {
return mb_strtoupper( mb_substr( $str, 0, 1, $enc ), $enc ) . mb_substr( $str, 1, mb_strlen( $str, $enc ), $enc );
} echo mb_ucfirst( "hello world 你好 中国" ) . PHP_EOL; //Hello world 你好 中国 /**
* @param $string
* @param string $encoding
*
* @return array
*/
function get_character_classes( $string, $encoding = "UTF-8" ) {
$current_encoding = mb_internal_encoding();
mb_internal_encoding( $encoding );
$has = array();
$stringlength = mb_strlen( $string, $encoding );
for ( $i = 0; $i < $stringlength; $i ++ ) {
$c = mb_substr( $string, $i, 1 );
if ( ( $c >= "0" ) && ( $c <= "9" ) ) {
$has['numeric'] = "numeric";
} else if ( ( $c >= "a" ) && ( $c <= "z" ) ) {
$has['alpha'] = "alpha";
$has['alphalower'] = 'alphalower';
} else if ( ( $c >= "A" ) && ( $c <= "Z" ) ) {
$has['alpha'] = "alpha";
$has['alphaupper'] = "alphaupper";
} else if ( ( $c == "$" ) || ( $c == "£" ) ) {
$has['currency'] = "currency";
} else if ( ( $c == "." ) && ( $has['decimal'] ) ) {
$has['decimals'] = "decimals";
} else if ( $c == "." ) {
$has['decimal'] = "decimal";
} else if ( $c == "," ) {
$has['comma'] = "comma";
} else if ( $c == "-" ) {
$has['dash'] = "dash";
} else if ( $c == " " ) {
$has['space'] = "space";
} else if ( $c == "/" ) {
$has['slash'] = "slash";
} else if ( $c == ":" ) {
$has['colon'] = "colon";
} else if ( ( $c >= " " ) && ( $c <= "~" ) ) {
$has['ascii'] = "ascii";
} else {
$has['binary'] = "binary";
}
}
mb_internal_encoding( $current_encoding ); return $has;
} $string = "1234asdfA£^_{}|}~žščř";
foreach ( get_character_classes( $string ) as $k => $v ) {
echo $k . " : " . $v . PHP_EOL;
}
//numeric : numeric
//alpha : alpha
//alphalower : alphalower
//alphaupper : alphaupper
//currency : currency
//ascii : ascii
//binary : binary

文章参考

转载注明出处

PHP之mb_substr使用的更多相关文章

  1. substr mb_substr mbstrct 的用法区别

    1.substr遇到中文会出问题,用于截取英文字符 2.mb_substr() 按字符截取字符串,需要开启php_mbstring.dll <?php echo mb_substr(, , 'u ...

  2. PHP截断函数mb_substr()详细介绍

    [导读] 在php中mb_substr()函数是用来截中文与英文的函数,可以方便快速的解决截取指定字符长度的问题,下面我来给大家介绍介绍.提示:mb_substr在于php中是默认不被支持的我们需要在 ...

  3. PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...

  4. PHP用substr截取字符串出现中文乱码问题用mb_substr

    PHP用substr截取字符串出现中文乱码问题用mb_substr实例:mb_substr('截取中文乱码问题测试',0,5, 'utf-8'); 语法 : string substr (string ...

  5. PHP中英文字符串截取函数无乱码(mb_substr)和获取中英文字符串字数函数(mb_strlen)

    mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $length,要截取的字数 $encod ...

  6. substr,mb_substr,iconv_substr,array_slice

    通过一个例子来看其关系 /** +---------------------------------------------------------- * 字符串截取,支持中文和其他编码 +----- ...

  7. php substr,iconv_substr,mb_substr

    php进行中文字符串的截取时,会经常用到二个函数iconv_substr和mb_substr,对这二个函数应该如何选择呢?参考下本文介绍的例子就明白了. 示例代码,用到了函数substr与iconv_ ...

  8. (转)PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...

  9. (转载)PHP mb_substr函数在实际编码中的应用方法

    (转载)http://developer.51cto.com/art/200912/166080.htm 我们在使用PHP语言进行实际编码中时,通常会遇到许多错误的出现,比如在截取字符串时会出现乱码等 ...

  10. (转载)PHP substr(),mb_substr()及mb_strcut的区别和用法

    (转载)http://blog.csdn.net/alongken2005/article/details/7098506 PHP substr()函数可以 分割文字,但要分割的文字如果包括中文字符往 ...

随机推荐

  1. [label][IDE] Develop Node.js Project With WebStorm

    WebStorm 是一个支持 Node.js,CoffeeScript, TypeScript, Dart, Jade, Sass, LESS and Stylus 这些最新 web 开发技术的集成开 ...

  2. LR中的迭代次数设置

    在参数化时,对于一次压力测试中均只能用一次的资源应该怎么参数化呢?就是说这些资源用了一次就不能在用了的. --参数化时,在select  next row选择unique,update value o ...

  3. linux apache安装https证书

    1.首先查看是否安装apache 命令:rpm -qa|grep httpd 如果存在,卸载命令:rpm -e XXXX 如果不存在,安装命令:yum install httpd 安装完apache之 ...

  4. ffmpeg学习(二) 通过rtsp获取H264裸流并保存到mp4文件

    本篇将使用上节http://www.cnblogs.com/wenjingu/p/3977015.html中编译好的库文件通过rtsp获取网络上的h264裸流并保存到mp4文件中. 1.VS2010建 ...

  5. c# Net XML文档(2,2)中有错误

    错误如图所示: xml转实体,需求很简单,度娘找了几个方法试了下,转换代码仔细看了看 没毛病啊  但是 就是提示 XML文档(2,2)中有错误,百度搜索了一大会 没解决方案,仔细分析了一下, 最后发现 ...

  6. UWP开发入门(四)——自定义CommandBar

    各位好,再次回到UWP开发入门系列,刚回归可能有些不适应,所以今天我们讲个简单的,自定义CommandBar,说通俗点就是自定义类似AppBarButton的东西,然后扔到CommandBar中使用. ...

  7. update sharepoint 2013 cu error

    1. 安装过程合理: A. 可以同时在管理中心.两台前端.搜索服务器上安装重新发布的SP1补丁包(所提供的链接) B. 等待所有SP1补丁包安装完成,依次在管理中心.两台前端.搜索服务器上运行配置向导 ...

  8. 在线编辑器Ckeditor (1) - php (30)

    在线编辑器 在线编辑器也称之为所见即所得编辑器,是一种常见的html源码编辑器. 所见即所得:用户在输入的时候,不论是格式和是样式都能被系统原封不动的保存,最后在查看的时候,可以按照用户输入的原来的结 ...

  9. 【OCP-12c】2019年CUUG OCP 071考试题库(76题)

    76.View the exhibit and examine the description of the DEPARTMENTSand EMPLOYEEStables. The retrieve ...

  10. 39.oracle高级篇

    标题说是高级篇,其实也就是相对于基础篇来说的,也不是很深奥,自己平时工作中也都会用到,这里回忆的并不是特别冷门的知识,不要掉以轻心,以为“高级”就觉得工作中不会用到了. 一.select into 和 ...