Home > Article > Backend Development > (Basics) PHP string functions
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.
1 Search character position function:
strpos($str,search,[int]): Find the first position of search in $str starting from int;
stripos($str, search,[int]): The function returns the position of the first occurrence of a string in another string. This function is not case sensitive strrpos($str,search,[int]): Find the last occurrence of search in $str starting from int
strripos($str,search,[int]): Same as above , this function is not case sensitive
2. Extract substring function (double bytes)
substr($str,int start[,int length] ): Extract [length-length string] starting from the strat position in $str.
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() has the same function as strstr, except that it is not case-sensitive.
strrchr() Returns from the last searched character; Use: Get the file name in the path
3. PHP string function to replace string
str_replace(search,replace,$str): Search search from $str and replace it with replace
str_ireplace(search,replace,$str): Same as above, this function is not case sensitive
strtr($str ,search,replace): replace in this function cannot be "";
substr_replace($Str,$rep,$start[,length]):$str original string,$rep new string after replacement,$ Start starting position, $length replacement length, 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 that is split into an array
str_split($str,len): Split $str according to the length of len and return the array
split(search,$str[, int]): Split $str according to the search character to 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 spaces 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 , HTML code related function
nl2br(): Convert \n to
.
strip_tags($str[,'
']): Remove HTML and PHP tags
All HTML and PHP codes in $str will be removed, optional parameters are html and PHP codes The effect 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, which is the 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): Convert single quotes ('), double quotes ("), backslashes (\) and NUL
strings in str for\',\",\\.
magic_quotes_gpc = On: Automatically escape the content of get, post, and cookie
get_magic_quotes_gpc(): Check whether magic_quotes_gpc is turned on
stripslashes(): Remove backslashes in the string
The above is the content of (Basics) PHP string functions. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!