In the programming process, string operations are very important and often used. Common Operations of strings include String concatenation, replacement of strings, search for strings, and comparison of strings, copy the string and calculate the length of the string.
- Concatenated string
Concatenating strings is one of the most common string operations. In PHP, You Can concatenate strings in three ways: dot. Separator {} 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.
- Replace 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])
- Calculate string
Calculate the length of a 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.
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. substr_count () syntax format of the function: int substr_count (string $ haystack, string $ needle [, int $ offset = 0 [, int $ length]) the parameters designed in the above syntax are described as follows: haystack specifies the string to be checked, needle is used to specify the string to be inserted, and offset is used to specify where to start searching in the string. The default value is 0, length is used to specify the length of the search.
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, and 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.
- 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.
Search for substrings: in PHP, The strstr () function can be used to search for substrings. The result returned by this function is all the contents of the first occurrence of the substring. 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 value 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.
Find the position of the string: The strpos () function is similar to the strstr () function, except that the returned value is not a string, but the position where a string appears for the first time in another string, the syntax format of strpos () is as follows: int strpos (string $ haystack, mixed $ needle [, int $ offset = 0]) describes the parameters involved in the preceding syntax 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.
- Comparison string
In PHP, comparing the size of two strings can be achieved in two ways: using the "=" operator to compare and using functions
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 (= ).
Use the function to compare the string size: The strcmp () function provided in PHP can more accurately compare the size of the two strings. 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 1 to be compared and str2 specifies the string 2 to be compared. 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 include the following three types. 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 the string to be compared ), 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)
- Copy 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 the str_repeat () function to copy strings, 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, multiplier specifies the number of times the string will be repeated.
- Flip string
The string processing operation also includes turning the string into the PHP language. The strrev () function can be used to reverse the string. strrev () the syntax format of the function is string strrev (string $ string)
The above parameter string is used to specify the string to be flipped.
- 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.
Split string: The explode () function is to split the string into arrays using the specified separator. The syntax format of the explode () function is as follows: array explode (string $ delimiter, string $ string [, int $ limit]) the parameters involved in the above syntax are described as follows: delimiter: specify where to separate strings, string: Specify the string to be separated, 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 ".
Merge string: 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 placed between array elements. The default value is "" (indicating a space string) pieces indicates the array to be merged into strings. 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.