Introduction to substr() function
The substr() function returns a part of a string.
Syntax: substr(string,start,length).
- 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 number - starts at the specified position from the end of the string; 0 - starts at the first character in the string.
- charlist: optional. Specifies the length of the string to be returned. The default is until the end of the string. Positive number - returned from the position of the start parameter; negative number - returned from the end of the string.
Note: If start is a negative number and length is less than or equal to start, length is 0.
Program List: negative start parameter
1 2 3 4 5 6 7 8 | <?php
$rest = substr ( "abcdef" , -1);
echo $rest . '<br />' ;
$rest = substr ( "abcdef" , -2);
echo $rest . '<br />' ;
$rest = substr ( "abcdef" , -3, 1);
echo $rest . '<br />' ;
?>
|
Copy after login
Program execution result:
Program List: negative length parameter
starts from the start position. If length is a negative value, it starts counting from the end of the string. If substr("abcdef", 2, -1), it starts from c, and then -1 means to intercept to e, which means to intercept cde.
1 2 3 4 5 6 7 8 9 10 | <?php
$rest = substr ( "abcdef" , 0, -1);
echo $rest . '<br />' ;
$rest = substr ( "abcdef" , 2, -1);
echo $rest . '<br />' ;
$rest = substr ( "abcdef" , 4, -4);
echo $rest . '<br />' ;
$rest = substr ( "abcdef" , -3, -1);
echo $rest . '<br />' ;
?>
|
Copy after login
Program execution result:
Program List: Basic substr() function usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php
echo substr ( 'abcdef' , 1);
echo '<br />' ;
echo substr ( 'abcdef' , 1, 3);
echo '<br />' ;
echo substr ( 'abcdef' , 0, 4);
echo '<br />' ;
echo substr ( 'abcdef' , 0, 8);
echo '<br />' ;
echo substr ( 'abcdef' , -1, 1);
echo '<br />' ;
$string = 'abcdef' ;
echo $string [0];
echo '<br />' ;
echo $string [3];
echo '<br />' ;
echo $string [ strlen ( $string )-1];
echo '<br />' ;
?>
|
Copy after login
Program execution result:
Program List: Remove suffix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php
function removeFromEnd( $string , $stringToRemove )
{
$stringToRemoveLen = strlen ( $stringToRemove );
$stringLen = strlen ( $string );
$pos = $stringLen - $stringToRemoveLen ;
$out = substr ( $string , 0, $pos );
return $out ;
}
$string = 'bkjia.jpg.jpg' ;
$result = removeFromEnd( $string , '.jpg' );
echo $result ;
?>
|
Copy after login
Program execution result:
Program List: If the string is too long, only the beginning and end will be displayed, and ellipses will be used instead
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php
$file = "Hellothisfilehasmorethan30charactersandthisfayl.exe" ;
function funclongwords( $file )
{
if ( strlen ( $file ) > 30)
{
$vartypesf = strrchr ( $file , "." );
$vartypesf_len = strlen ( $vartypesf );
$word_l_w = substr ( $file ,0,15);
$word_r_w = substr ( $file ,-15);
$word_r_a = substr ( $word_r_w ,0,- $vartypesf_len );
return $word_l_w . "..." . $word_r_a . $vartypesf ;
}
else
return $file ;
}
$result = funclongwords( $file );
echo $result ;
?>
|
Copy after login
Program execution result:
1 | Hellothisfileha...andthisfayl.exe
|
Copy after login
Program List: Display extra text as ellipsis
Many times we need to display a fixed number of words, and the extra words are replaced with ellipses.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$text = 'welcome to bkjia, I hope you can find something you wanted.' ;
$result = textLimit( $text , 30);
echo $result ;
function textLimit( $string , $length , $replacer = '...' )
{
if ( strlen ( $string ) > $length )
return (preg_match( '/^(.*)\W.*$/' , substr ( $string , 0, $length +1), $matches ) ? $matches [1] : substr ( $string , 0, $length )) . $replacer ;
return $string ;
}
?>
|
Copy after login
Program execution result:
1 | welcome to bkjia, I hope...
|
Copy after login
Program List: Format string
Sometimes we need to format strings, such as phone numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php
function str_format_number( $String , $Format )
{
if ( $Format == '' ) return $String ;
if ( $String == '' ) return $String ;
$Result = '' ;
$FormatPos = 0;
$StringPos = 0;
while (( strlen ( $Format ) - 1) >= $FormatPos )
{
if ( is_numeric ( substr ( $Format , $FormatPos , 1)))
{
$Result .= substr ( $String , $StringPos , 1);
$StringPos ++;
}
else
{
$Result .= substr ( $Format , $FormatPos , 1);
}
$FormatPos ++;
}
return $Result ;
}
$String = "8607562337788" ;
$Format = "+00 0000 0000000" ;
echo str_format_number( $String , $Format );
echo '<br />' ;
$String = "8607562337788" ;
$Format = "+00 0000 00.0000000" ;
echo str_format_number( $String , $Format );
echo '<br />' ;
$String = "8607562337788" ;
$Format = "+00 0000 00.000 a" ;
echo str_format_number( $String , $Format );
echo '<br />' ;
?>
|
Copy after login
Program execution result:
1 2 3 | +86 0756 2337788
+86 0756 23.37788
+86 0756 23.377 a
|
Copy after login
http://www.bkjia.com/PHPjc/752408.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752408.htmlTechArticlesubstr() function introduction The substr() function returns a part of a string. Syntax: substr(string,start,length). string: required. Specifies a part of the string to be returned. start: Must...