How to intercept string in php

silencement
Release: 2023-02-24 20:58:01
Original
8393 people have browsed it

How to intercept string in php

php has a large number of built-in string manipulation functions, such as php implode, explode, etc. To intercept strings in php, you can use the substr and mb_substr functions.

phpsubstr syntax

substr(string, start, length)
Copy after login

Parameters

string   即要截取的字符串
start   即要截取的开始位置(0表示从从前往后数 第一个字符开始,负数表示从从后往前数)
lengthlength 当为正数时,为需要截取的长度;当为负数时,即理解为去掉末尾的几个字符
Copy after login

For example

$str1 = substr("abcdef", 1);    // 返回 "bcdef"
$str2 = substr("abcdef", 2);    // 返回 "cdef"
$str3 = substr("abcdef", 0,1);    // 返回 "a"
$str4 = substr("abcdef", 0,2);    // 返回 "ab"
$str5 = substr("abcdef", -1); // 返回 "f"
$str6 = substr("abcdef", -2); // 返回 "ef"
$str7 = substr("abcdef", 0,-1); // 返回 "abcde"
$str8 = substr("abcdef", 0,-2); // 返回 "abcd"
$str9 = substr('你好你好你好',1);//返回:??好你好你好
$str10 = mb_substr('你好你好你好',1);//返回:好你好你好
Copy after login

Careful children may have discovered that substr in $str9 returns � �Hello hello hello, there are Chinese garbled characters appearing.

Substr is often used in PHP to intercept strings, but when we use it to intercept Chinese characters, garbled characters will occur. At this time, we need to use another

One function, mb_substr, now the problem of garbled characters is solved.

The above is the detailed content of How to intercept string in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template