Method: 1. Use the "substr (string, starting position, interception length)" statement; 2. Use the "mb_substr (string, starting position, interception length, encoding)" statement; 3. Use " mb_strcut (string, starting position, interception length, encoding)" statement; 4. Use the "$str[character position]" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
substr(string, starting position , intercept length)
If the intercepted string is multiple bytes, garbled characters will appear, such as Chinese, suitable for intercepting letters and numbers
$str = 'hello'; echo substr($str,1,2);//输出el
mb_substr( String, starting position, interception length, character encoding)
Suitable for intercepting Chinese, segmented by characters
echo mb_substr('这个真的很nice',0,3,'utf-8'); //输出这个真
mb_strcut(String, starting position, interception Length, character encoding)
Split by bytes
echo mb_strcut('这个真的很nice',0,3,'utf-8'); //输出这
$str[0]
Treat strings as characters Collection, Chinese is not applicable!
$str = 'hello'; echo $str[0];//输出h
Sometimes the string contains Chinese and English numbers, etc., so customize a function that does not distinguish between Chinese and English to split the string
/** * * 中英混合的字符串截取 * @param 待截取字符串 $sourcestr * @param 截取长度 $cutlength */ function sub_str($sourcestr, $cutlength) { $returnstr = '';//待返回字符串 $i = 0; $n = 0; $str_length = strlen ( $sourcestr ); //字符串的字节数 while ( ($n < $cutlength) and ($i <= $str_length) ) { $temp_str = substr ( $sourcestr, $i, 1 ); $ascnum = Ord ( $temp_str ); //得到字符串中第$i位字符的ascii码 if ($ascnum >= 224) {//如果ASCII位高与224, $returnstr = $returnstr . substr ( $sourcestr, $i, 3 ); //根据UTF-8编码规范,将3个连续的字符计为单个字符 $i = $i + 3; //实际Byte计为3 $n ++; //字串长度计1 } elseif ($ascnum >= 192){ //如果ASCII位高与192, $returnstr = $returnstr . substr ( $sourcestr, $i, 2 ); //根据UTF-8编码规范,将2个连续的字符计为单个字符 $i = $i + 2; //实际Byte计为2 $n ++; //字串长度计1 } elseif ($ascnum >= 65 && $ascnum <= 90) {//如果是大写字母, $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; //实际的Byte数仍计1个 $n ++; //但考虑整体美观,大写字母计成一个高位字符 }elseif ($ascnum >= 97 && $ascnum <= 122) { $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; //实际的Byte数仍计1个 $n ++; //但考虑整体美观,大写字母计成一个高位字符 } else {//其他情况下,半角标点符号, $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; $n = $n + 0.5; } } return $returnstr; }
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What are the methods for intercepting php strings?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
