Home >Backend Development >PHP Tutorial >Usage of substr() and explode() functions in php

Usage of substr() and explode() functions in php

怪我咯
怪我咯Original
2017-07-06 11:22:461408browse

This article mainly introduces the usage analysis of substr() and explode() functions in PHP, and describes substr in detail in the form of examples. () and explode() function processing String skills are relatively 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" 
?>

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) 
?>


The above is the detailed content of Usage of substr() and explode() functions in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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