Explore the summary analysis of various PHP string functions

  • 2020-06-07 04:06:52
  • OfStack

PHP string functions include find character position functions; Extract subcharacter function; Replace the string; Character length; Compare character functions; Split into array characters; Remove Spaces and so on.

String functions in THE PHP language are also an easy to understand knowledge. Today we have summarized nearly 12 kinds of PHP string functions for you, hoping to be helpful to friends who need it again and increase readers' PHP knowledge base.


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 case-insensitive strrpos($str,search,[int]): Find the last occurrence of search in $str starting with int
strripos($str,search,[int]): As above, this function is case insensitive

2. Extract substring function (double byte)
substr($str,int start[,int length]): Extracts [length length string] from the strat position in $str.
strstr($str1,$str2): Search $str2 from $str1(the first position) and intercept the end string from it; If not, return FALSE.
stristr() functions the same as strstr, only case-insensitive.
strrchr() returns the character from the last search; Use: Takes the filename in the path

PHP string function to replace the string
str_replace(search,replace,$str): Look for search in $str and replace it with replace
str_ireplace(search,replace,$str): Ibid., this function is case insensitive
strtr($str,search,replace): replace cannot be "" in this function;
substr_replace($Str,$rep,$start[,length]):$str original string,$rep replaced with a new string,$start started,$length replaced with a length that is optional

4. Character length
int strlen($str)

5. Compare character functions
int strcmp($str1,$str2):$str1 > = < $str2 + 1,0,-1 (string comparison)
strcasecmp() ibid. (case insensitive)
strnatcmp("4","14") compares strings in a natural order
strnatcasecmp() ibid., (case sensitive)

6, PHP string function into an array
str_split($str,len): Split $str into len lengths to return an array
split(search,$str[,int]): Split $str by search character return array int is split several times, followed by expload(search,$str[,int])

7. Remove Spaces:
ltrim(), rtrim(), trim()

8, add space function
chunk_split($str,2): Add 1 space per 2 characters to the $str character;

9, chr, ord-- returns the specified character or ascii

10, HTML code related functions
nl2br() : Convert \n to < br > .
strip_tags($str[,' < p > ') : Remove HTML and PHP tags

In $str, all HTML and PHP codes will be removed, and the optional html and PHP codes will keep the code written with the optional parameters.
echo strip_tags($text, ') < br > < p > ');
htmlspecialchars($str[, parameters]): The page output HTML code parameters, is the conversion mode

PHP string function for character capitalization conversion
strtolower($str): Converts a string to lowercase
strtoupper($str): Converts a string to uppercase
ucfirst($str): Converts the first character of the function to uppercase
ucwords($str): Converts the first letter of each word to uppercase

12, database related PHP string function
addslashes($str) : Makes single quotes ('), double quotes ("), backslash (\) and NUL within str
String converted to \',\",\\.
magic_quotes_gpc = On: Automatically escapes the contents of get, post, cookie
get_magic_quotes_gpc () : Checks whether magic_quotes_gpc is turned on
stripslashes(): Removes backslashes from a string


Related articles: