PHP substr () function
Definition and Usage
substr () function returns part of a string.
Note: If thestart argument is negative and the length is less than or equal to start, then the length is zero.
grammar
substr( string,start,length )
参数 | 描述 |
---|---|
string | 必需。规定要返回其中一部分的字符串。 |
start | 必需。规定在字符串的何处开始。
|
length | 可选。规定要返回的字符串长度。默认是直到字符串的结尾。
|
technical details
return value: | Back extract part of the string, if it fails it returns FALSE, or returns an empty string. |
---|---|
PHP version: | 4+ |
Update log: | In PHP 5.2.2 to 5.2.6 version, if a negative start parameter indicates the position of cross-border or cut off, it returns FALSE. Other versions from the start position start getting the string. |
More examples
Example 1
Use the start parameters with different positive and negative numbers:
<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
Running instance »
Example 2
Use start and length parameters with different positive and negative numbers:
<?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>";
?>
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>";
?>
Running instance »
PHP String Reference