Analysis on usage of substr function string interception in PHP, phpsubstr
This article describes the usage of substr function string interception in PHP with examples. Share it with everyone for your reference, the details are as follows:
The substr function in PHP is defined as follows:
substr(string,start,length)
The parameter description is as follows:
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 returned string. The default is until the end of the string.
Positive number - the length returned from the position of the start parameter
Negative number - the length returned from the end of the string
The sample code is as follows:
<?php
echo substr("Welcome to www.jb51.net!",0); //原样输出,不截取
echo "<br>";
echo substr("Welcome to www.jb51.net!",4,14); //从第4个字符开始连续截取14个字符
echo "<br>";
echo substr("Welcome to www.jb51.net!",-4,4); //从倒数第4个开始截取4个字符
echo "<br>";
echo substr("Welcome to www.jb51.net!",0,-4); //从第一个字符开始截取,截取到倒数第4个字符
?>
Copy after login
The running results are as follows:
Welcome to www.jb51.net!
ome to www.jb5
net!
Welcome to www.jb51.
Copy after login
I hope this article will be helpful to everyone in PHP programming.
Articles you may be interested in:
- How to obtain the number of occurrences of a substring with the substr_count() function in PHP
- After PHP uses the strstr() function to obtain the specified string Methods for all characters
- The strncmp() function in PHP compares whether the first 2 characters of two strings are equal
- The strnatcmp() function "natural sorting algorithm" in PHP performs string comparison Usage analysis (compared with strcmp function)
- Strcmp() and strcasecmp() function string comparison in PHP Usage analysis
- Example of PHP using trim function to remove left and right spaces and special characters from a string
- PHP encapsulated string encryption and decryption function
- The most accurate PHP interception string length function
- What to do if the Chinese garbled problem occurs when substr() is used to intercept strings in PHP
- Summary of examples of common string manipulation functions in PHP
http://www.bkjia.com/PHPjc/1089581.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1089581.htmlTechArticleAnalysis on the usage of substr function string interception in PHP, phpsubstr This article describes the usage of substr function string interception in PHP. Share it with everyone for your reference, the details are as follows: subs in PHP...