There are many string functions in PHP, among which the string splitting function is very commonly used. The string split function can split a string according to the specified delimiter and return an array. Below we will introduce several commonly used string splitting functions.
- explode function
The explode function can split the string according to the specified delimiter and return an array. The syntax is as follows:
explode(string $separator , string $string , int $limit = PHP_INT_MAX)
Parameter explanation:
- $separator: required, specifies the separator, which can be a string or a string array.
- $string: Required, specifies the string to be split.
- $limit: Optional, specifies the maximum length of the returned array. Default is PHP_INT_MAX.
Sample code:
$str = "apple,banana,pear,orange"; $arr = explode(",", $str); print_r($arr);
Output result:
Array ( [0] => apple [1] => banana [2] => pear [3] => orange )
- str_split function
The str_split function can convert the string according to Divides by the specified length and returns an array. The syntax is as follows:
str_split ( string $string , int $split_length = 1 )
Parameter explanation:
- $string: Required, specify the string to be split.
- $split_length: Optional, specify the length of each element, the default is 1.
Sample code:
$str = "hello world"; $arr = str_split($str); print_r($arr);
Output result:
Array ( [0] => h [1] => e [2] => l [3] => l [4] => o [5] => [6] => w [7] => o [8] => r [9] => l [10] => d )
- strtok function
The strtok function can convert the string according to Split with the specified delimiter and return the first split substring. The syntax is as follows:
strtok ( string $string , string $token )
Parameter explanation:
- $string: Required, specify the string to be split.
- $token: required, specify the delimiter.
Sample code:
$str = "apple,banana,pear,orange"; $tok = strtok($str, ","); while ($tok !== false) { echo "$tok<br>"; $tok = strtok(","); }
Output result:
apple banana pear orange
Through the above example, we can see that using the string splitting function can quickly and easily split strings for processing. In actual development, we need to choose different string splitting functions according to different needs to achieve the best processing effect.
The above is the detailed content of PHP string function example: string splitting. For more information, please follow other related articles on the PHP Chinese website!

两种去除方法:1、使用rtrim()函数,语法“rtrim(数值,"0")”,可去除数值后面连续的零。2、用substr_replace(),语法“substr_replace(数值,'', -位置值)”,可从指定位置开始将零替换为空字符。

方法:1、用str_replace(),语法“str_replace(" "," ",$str)”;2、用preg_replace()配合正则表达式,语法“preg_replace('/\s+/'," ",$str)”。

php将小数点转为百分比的方法:1、利用“*”运算符将小数乘以100,语法“小数 * 100”,可将小数转为百分值;2、使用字符串拼接符“.”将百分值和“%”符拼接在一起,形成百分比字符串即可,语法“百分值."%"”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

转换方法:1、使用“array_merge_recursive(数组1,数组2,数组3...)”语句将多个数组合并为一个数组;2、使用json_encode()将合并后的数组转为json数据,语法“json_encode(合并后的数组)”。

在php中,可以利用str_replace()函数来去除字符串中的多个指定子字符串,只需要将该函数的第一个参数设为包含多个值的数组,第二个参数设为空字符即可;语法“str_replace(数组,'',$str)”,会将多个子串替换为空字符。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
