Common string operation functions in PHP development, and string functions in php Development
1. concatenate strings
Concatenating strings is one of the most common string operations. In PHP, You Can concatenate strings in three ways: Dots. separator {} operation, and dot equal sign. =. The dot equals sign can be used to break down a long string into several rows for definition. This is advantageous.
2. Replace the string
In the PHP language, a function called substr_replace () is provided. This function can quickly scan and edit strings with more content. syntax format:
Mixed substr_replace (mixed $ string, string $ replacement, int $ start [, int $ length])
The preceding syntax format is described as follows:
String is the string to be checked or to be replaced
Replacement specifies the string to be inserted or replaced
Start specifies where to start replacement of the string. This parameter can be set to three types of values (positive number: Starting from the start offset at the beginning of the string, negative number, starting from the start offset at the end of the string, 0: starting from the first character in the string)
Length specifies the number of characters to be replaced. This parameter can also be set to three types of values (positive number: length of the string to be replaced, negative number: the length of the string to be replaced from the end of the string. 0 indicates the insert operation, not the replacement operation );
3. Calculate the string
1) Calculate the length of the string
In PHP, The strlen () function is used to calculate the length of a string and return the length information of the string. the syntax format is as follows: the string in the int strlen (string $ string) format is used to specify the string to calculate the length.
2) calculate the number of strings
In PHP, The substr_count () function can be used to conveniently and accurately determine the number of specified substrings in the provided strings. The syntax format of the substr_count () function is as follows: int substr_count (string $ haystack, string $ needle [, int $ offset = 0 [, int $ length]) the parameters designed in the preceding syntax are described as follows: haystack specifies the string to be checked. needle is used to specify the string to be inserted. offset is used to specify where to start searching in the string. The default value is 0. length is used to specify the search length.
In PHP, The str_word_count () function can be used to conveniently and accurately determine the number of characters in a provided string. The syntax format of the str_word_count () function is as follows: mixed str_word_count (string $ string [, int $ format = 0 [, string $ charlist]) the parameters involved in the preceding syntax are described as follows: string is used to specify the string to be checked, and format is used to specify the return value of the str_word_count () function. The return value of this parameter can return three values, respectively, 0, 1, 2. 0 indicates the default value. The number of words found is returned. If the return value is 1, str_word_count () returns an array, the key name is a continuous integer starting from 0, and the value is the actual word. if the value of format is 3, the return value of the str_word_count () function is an array. The key name of the array is the position of the word in the string, and the value is the actual word.
4. Search for strings
String search can be divided into many types, such as searching for substrings and finding the position of a string. PHP provides corresponding functions for each string search operation.
1) Search for substrings
In PHP, The strstr () function can be used to search for a substring. The result returned by this function is all the content of the substring that appears for the first time. The format of the strstr () function is as follows: string strstr (string $ haystack, mixed $ needle) in the above syntax, the parameters involved are described as follows: haystack: Specifies the string to be searched, needle specifies the string to be searched, if this parameter is a number, it will match the ASCII character of the number. in actual applications, case-insensitive letters may be ignored. In this case, you can use the stristr () function, a case-insensitive lookup function provided by PHP, this function is used in the same way as the strstr () function.
2) locate the string
The strpos () function is similar to the strstr () function, but returns not a string, but the position where a string appears for the first time in another string. strpos () syntax format for example: int strpos (string $ haystack, mixed $ needle [, int $ offset = 0]). The parameters involved in the preceding syntax are described as follows: haystack is the string to be searched. needle specifies the string to be searched, and offset specifies the start position. The default value is 0.
The strpos () function is a case-sensitive lookup function. However, in actual application, case-sensitive queries are often ignored, in this case, you can use stripos (), a case-insensitive search function provided by PHP. This function is used in the same way as strpos.
5. compare strings
In PHP, comparing the size of two strings can be achieved in two ways: using the "=" operator to compare and using functions
1) use the "=" operator to compare the size of two strings
When comparing two strings in PHP, the easiest way is to use the equal sign operator (= ).
2) use a function to compare the string size
The strcmp () function provided in PHP can compare the size of two strings more accurately. The syntax format is as follows: int strcmp (string $ str1, string $ str2) the parameters involved in the preceding syntax are described as follows: str1 specifies the string to be compared 1, str2 specifies the string to be compared 2. the strcmp signature is used to ensure that the two strings match completely and return the comparison result in the form of an integer. The return values of this function are as follows. 0: The two strings are equal. When the value is less than 0, the first string is smaller than the subsequent string. If the return value is greater than zero, it indicates that, the preceding string is greater than the following string.
In addition to the strcmp () function, PHP also provides some similar comparison functions. For example, the strncmp () function can select the length (number of characters) of a string to be greater than that of a string ), the syntax format is as follows: int strcmp (string $ str1, string $ str2, int $ len) the preceding parameter description str1: Specifies the first string to be compared, str2: specify the second string to be compared len: specify the number of characters each string uses for comparison.
When comparing strings, you can use strcasemp () and strncasemp () functions to ignore case sensitivity, these two functions are used exactly the same as the case-sensitive functions. The syntax formats of strcasecmp () and strncasecmp () functions are as follows:
Int strcasecmp (string $ str1, string $ str2)
Int strncasecmp (string $ str1, string $ str2, int $ len)
6. Copy the string
If you need to repeatedly display a character or a string for n times, the simplest method is to call the copy function. In PHP, you can use str_repeat () the function implements string reproduction. The syntax format of this function is as follows: string str_repeat (string $ input, int $ multiplier) description of the parameters designed in the preceding Syntax: input specifies the string to be repeated, and multiplier specifies the number of times the string will be repeated.
7. Flip the string
The operations to process strings include turning the strings into PHP. The strrev () function can be used to reverse the string. The syntax format of strrev () function is
String strrev (string $ string)
The string parameter above is used to specify the string to be flipped.
8. Split and merge strings
Splitting a string into multiple strings according to certain rules, or merging multiple strings into a long string is a common problem during string operations. use the explode () function provided by PHP, str_split () function, and implode () function to handle similar splitting and string merging issues.
1) split the string
The function of the explode () function is to split the string into arrays using the specified delimiter. The syntax format of the explode () function is as follows: array explode (string $ delimiter, string $ string [, int $ limit]) the parameters involved in the preceding syntax are described as follows: delimiter: Specifies the separator string, string: Specifies the string to be split, limit: specify the maximum number of returned array elements. The final sub-block will contain the rest of the string.
The str_split () function is used to split a string into multiple sub-strings of equal length. the syntax format of the str_split () function is as follows: array str_split (string $ string [, int $ split_length = 1]) the parameters in the preceding syntax are described as follows: string: Specifies the string to be split, split_length: specify the length of each array element. The default value is "1 ".
2) Merge strings
The implode () function connects the elements of the array to a string. The syntax format of the implode () function is as follows: string implode ([string $ glue], array $ pieces) the parameters involved in the preceding syntax are described as follows: glue () specifies the content to be placed between array elements. The default value is "" (indicating a space string) pieces specifies the array to be merged as a string. you can call the implode () function to obtain a new string based on the conditions specified by the parameter to merge the strings.
Join () is the alias of the implode () function. The two functions are used in the same way. It should be emphasized that although the glue parameter is optional, however, two parameters are recommended for better program compatibility.
To sum up the operation methods of the string:
1) printf () function: You can format strings.
2) sprintf () function: You can format a string. The difference between sprintf () and printf () is that you need to use echo to display and output formatted strings.
3) nl2br () function: You can convert the linefeed in the string "\ n" to "<br/>" in HTML and display it.
4) wordwrap () function: Specifies to force line feed from a column of Characters
5) strtolower () function: This function converts all characters in a string to lowercase characters.
6) strtoupper () function: This function converts all the characters in the string to uppercase characters.
7) ucwords () function: converts all the first characters in a string to uppercase characters.
8) substr_replace () function: This function allows you to quickly scan and edit strings with more text content.
9) strlen () function: calculates the length of a string and returns the length of the string.
10) substr_count () function: determines the number of characters in a string provided.
11) The str_word_count () function determines the number of times a word appears in a string.
12) strstr () function, which can be used to search for substrings. The result returned by this function is all the content after the first occurrence of the substrings.
13) The strpos () function is similar to the strstr () function, but returns the position of a string in another string rather than a string.
14) strcmp () function, which can accurately compare the size of two strings
15) strncmp () function. You can select the length (number of characters) of the string to be compared)
16) strcasecmp () function, which can be used to compare two strings when case sensitivity is ignored.
17) The strncasecmp () function can compare the length or number of characters of two strings without case sensitivity.
18) str_repeat () function, which repeatedly displays one character or string n times
19) strrev () function, which allows you to flip strings.
20) The explode () function allows you to split a string into multiple strings. Specify the delimiter to split the string into arrays.
21) str_split () function, which can split a string into multiple sub-strings of equal length
22) the implode () function connects the elements of the array to a string.
23) the join () function uses the same method as the implode () function. Its function is to concatenate array elements into a string.