php Easy Mixed String Interception

  • 2021-06-28 11:49:19
  • OfStack

Let's start by introducing the functions used:


mb_strwidth($str, $encoding)  Returns the width of the string 
$str  String to calculate 
$encoding  Encoding to use, such as  utf8 , gbk


mb_strimwidth($str, $start, $width, $tail, $encoding)  Intercept strings by width 
$str  String to intercept 
$start  From which location to intercept, default is 0
$width  Width to intercept 
$tail  A string appended to the back of the intercept string, usually  ...
$encoding  Encoding to use 

Below is an example:


<?php
/**
 * utf8 编码格式
 * 1个中文占用3个字节
 * 我们希望的是1个中文占用2个字节,
 * 因为从宽度上看2个英文字母占用的位置相当于1个中文
 */
// 测试字符串
$str = 'aaaa啊啊aaaa啊啊啊aaa';
echo strlen($str); // 只用strlen输出为25个字节
// 必须指定编码,不然会使用php的内码 mb_internal_encoding()可以查看内码
// 使用mb_strwidth输出字符串的宽度为20使用utf8编码
echo mb_strwidth($str, 'utf8'); 
// 只有宽度大于10才截取
if(mb_strwidth($str, 'utf8')>10){
    // 此处设定从0开始截取,取10个追加...,使用utf8编码
    // 注意追加的...也会被计算到长度之内
    $str = mb_strimwidth($str, 0, 10, '...', 'utf8');
}
// 最后输出 aaaa啊... 4个a算4个 1个啊算2个 3个点算3个 4+2+3=9
// 是不是很简单啊,有的人说了为什么是9个不是10个吗?
// 因为正好“啊”的后边还是“啊”,中文算2个,9+2=11 超出了设定,所以去掉1个就是9了
echo $str;

Here are some other functions:


mb_strlen($str, $encoding)  Returns the length of the string 
$str  String to calculate 
$encoding  Coding used 


mb_substr($str, $start, $length, $encoding)  substr 
$str  String to intercept 
$start  Where to start intercepting 
$length  How long to intercept 
$encoding  Coding used 

These two functions are similar to strlen() and substr(), but the only difference is that they can be coded.

Instance below:


<?php
/**
 * utf8  Encoding Format 
 * 1 Chinese Occupancy 3 Bytes 
 */
$str = 'aa12 ah aa';
echo strlen($str); //  Direct output length is 9
//  Output length is 7 Why? 7 And? 
//  Note that when the encoding is set here, every length is 1
// a a 1 2  ah  a a 
// 1+1+1+1+1+1+1 = 7
//  Is that right 7 Characters 
echo mb_strlen($str, 'utf8');
//  same mb_substr Also 1 Sample 
//  I just want to 5 Characters 
echo mb_substr($str, 0, 5, 'utf8'); //  output  aa12 ah 

In fact, there are a lot of useful functions inside the mb extension, here we will not list them for you 11.

Interested friends can view the official Brochure

Okay, I'm here today.


Related articles: