Home > Article > Backend Development > Discuss the summary analysis of various PHP string functions_PHP tutorial
PHP string functions include find character position functions; extract sub-character functions; replace strings; character length; compare character functions; split into array characters; remove spaces, etc.
The string functions in PHP language are also relatively easy to understand. Today we have summarized nearly 12 PHP string functions for you. We hope it will be helpful to friends who need them again and increase the PHP knowledge base of readers and friends.
2. Extract substring function (double byte)
substr($str,int start[,int length]):From the strat position in $str Start extracting [length length string].
strstr($str1,$str2):Search $str2 from $str1 (the first position) and intercept it from the beginning to the end of the string; if not, return FALSE.
stristr() The function is the same as strstr, but it is not case-sensitive.
strrchr() Returns from the last searched character; Use: Get the file name in the path
3. PHP string function for replacing strings
str_replace(search,replace,$str):Find search from $str and replace with replace
str_ireplace(search,replace,$str):Same as above, this function is not case sensitive
strtr($str,search,replace):replace cannot be used in this function is "";
substr_replace($Str,$rep,$start[,length]):$str original string, $rep replaced new string, $start starting position, The length of $length replacement, this item is optional
4. Character length
int strlen($str)
5. Comparison character function
int strcmp($str1,$str2):$str1>=<$str2 are positive 1,0,-1 respectively (String comparison)
strcasecmp() Same as above (case-insensitive)
strnatcmp("4","14") Compare strings in natural order
strnatcasecmp() Same as above, (case sensitive)
6. PHP string function split into arrays
str_split($str,len):Split $str according to the length of len and return the array
split(search,$str[,int]):Split $str according to search characters and return the array int. How many times will it be split? The rest will not be split.expload(search,$str[,int ])
7. Remove spaces:
ltrim(), rtrim(), trim()
8. Add space function
chunk_split($str,2):Press 2 characters into the $str character to add a space;
9, chr, ord--return the specified character or ascii
10. Functions related to HTML code ']):
nl2br(): convert n to
.
strip_tags($str[,'
All HTML and PHP codes in $str will be removed. The optional parameters are html and PHP codes. The function of the optional parameters is to retain the code written by the optional parameters.
For example: echo strip_tags($text, '
');
htmlspecialchars($str[,parameter]):The page normally outputs HTML code parameters, yes Conversion method
11. PHP string function for character case conversion
strtolower($str): Convert string to lowercase
strtoupper($str ): Convert string to uppercase
ucfirst($str): Convert the first character of the function to uppercase
ucwords($str): Convert the first letter of each word to uppercase
12. Database-related PHP string functions
addslashes($str): Enable single quotes ('), double quotes ("), and backslashes in str Line() with NUL
string converted to ',",\.
magic_quotes_gpc = On: Automatically escape the contents of get, post, and cookie
get_magic_quotes_gpc(): Detect whether magic_quotes_gpc is turned on
stripslashes( ): Remove backslashes from strings