Detailed explanation of usage examples of substr() function and explode() function in php

怪我咯
Release: 2023-03-13 06:42:01
Original
1395 people have browsed it

This article mainly introduces the usage analysis of substr() and explode() functions in PHP, and describes substr in more detail in the form of examples. () and explode() functions are techniques for processing strings. They are frequently used functions in string operations and have certain practical value. Friends in need can refer to this article

The example describes the usage of substr() and explode() functions in PHP. Share it with everyone for your reference. The specific method is as follows:

substr(

string,start,length): This function takes out length characters from the string starting from the start position of the string. If start is a negative number, then Counting from the end of the string, if the omitted parameter length exists but is a negative number, it means that the penultimate length character is obtained. The example code is as follows:

The code is as follows:

<?php 
echo substr("abcdef", 1, 3);  // 返回 "bcd" 
echo substr("abcdef", -2);    // 返回 "ef" 
echo substr("abcdef", -3, 1); // 返回 "d" 
echo substr("abcdef", 1, -1); // 返回 "bcde" 
?>
Copy after login

explode(separator,string,limit): This function splits the string into

array separator specifies where to split, string the string to be split, limit optional, the maximum number of array elements, example The code is as follows:

The code is as follows:

<?php 
 $str = "abc|bck|fhq|nui"; 
   print(explode("|",$srt)); //输出array([0]=>abc,[1]=>bck,[2]=>fhq,[3]=>nui) 
?>
Copy after login


The above is the detailed content of Detailed explanation of usage examples of substr() function and explode() function 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!