Home > Article > Backend Development > How to split a string in php and take the first few digits
In PHP, you can use the substr() function to split a string and take the first few characters. This function can return a part of the specified string. You can use parameters to specify where to start splitting and what to cut. The length, the syntax is "substr(string, 0, first few digits)".
The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer
The substr() function returns a part of a string.
Note: If the start parameter is negative and length is less than or equal to start, length is 0.
Syntax
substr(string,start,length)
The parameter settings are as shown in the following table:
The example is as follows:
<?php echo substr("Hello world",0,10)."<br>"; echo substr("Hello world",1,8)."<br>"; echo substr("Hello world",0,5)."<br>"; echo substr("Hello world",6,6)."<br>"; echo substr("Hello world",0,-1)."<br>"; echo substr("Hello world",-10,-2)."<br>"; echo substr("Hello world",0,-6)."<br>"; echo substr("Hello world",-2-3)."<br>"; ?>
Output results:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to split a string in php and take the first few digits. For more information, please follow other related articles on the PHP Chinese website!