Return "world" from a string:
<?phpecho substr("Hello world",6);?>The substr() function returns a portion of a string.
Note: If the start parameter is negative and length is less than or equal to start, length is 0.
substr( string,start,length )
parameter | describe |
---|---|
string | Required. Specifies a part of the string to be returned. |
start | Required. Specifies where in the string to begin. Positive number - starts at the specified position in the string Negative numbers - start at the specified position from the end of the string 0 - Start at the first character in the string |
length | Optional. Specifies the length of the string to be returned. The default is until the end of the string. Positive number - returns from the position of the start parameter Negative number - returned from the end of the string |
Return value: | Returns the extracted portion of the string, or FALSE on failure, or an empty string. |
---|---|
PHP version: | 4+ |
Update log: | In PHP versions 5.2.2 to 5.2.6, if the start parameter represents a negative truncation or an out-of-bounds position, FALSE is returned. Other versions get the string starting from the start position. |
Use start parameters with different positive and negative numbers:
<?phpecho 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>";? >Use start and length parameters with different signs:
<?phpecho 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>";?>