In the process of programming, the manipulation of strings is very important and often used, the string commonly used in the operation of the specific string concatenation, replacement strings, find strings, compare strings, copy strings and calculate the length of the string and so on.
- Stitching strings
Stitching strings is one of the most commonly used string operations, and in PHP it supports three ways to stitch strings, namely dots. The delimiter {} operation, and the dot equals sign. = to operate, the dot equals can be used to decompose a long string into a few lines to define, it is more advantageous to do so.
- Replace string
In the language of PHP, a function named Substr_replace () is provided, which can be used to quickly scan and edit the string substitution function with more text content. His 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 for that string. The syntax is formatted as follows: string in the format int strlen (string $string) is used to specify a string to calculate the length.
Count the number of strings: in PHP, the Substr_count () function can be used to make it very convenient and accurate to determine how many of the specified substrings are in the provided string, and the syntax of the Substr_count () function is as follows: Int Substr_count ( String $haystack, String $needle [, int $offset =0[,int $length]) The parameters that are designed in the syntax above are described as follows: haystack specifies the string to check, Needle is used to specify the string to insert, offset is used to specify where to start the search in the string, and the default value is 0,length to specify the length of the search.
In PHP, the Str_word_count () function can be used to make it very convenient and accurate to determine how many of a word is in the provided string, and the syntax of the Str_word_count () function is as follows: Mixed Str_word_count ( string $string [, int $format =0[,string $charlist]]) The arguments involved in the above syntax are described as follows: string specifies the string to be checked, and format is used to specify Str_word_count () function, the return value of this parameter can return three values, respectively, 0,1,2. Which returns 0 means the default value, returns the number of words found, and if the return value is 1, then Str_word_count () returns an array where the key name is a sequential integer starting at 0, and the value is the actual word. If the value of format is 3, then the return value of the Str_word_count () function is an array where the key name of the array is the position of the word in the string, and the value is the actual word.
- Find string
String lookup can be divided into many kinds, such as finding substrings, finding the location of a string, and so on, PHP provides the corresponding function for each string lookup operation.
Find substrings: In the PHP language, the STRSTR () function can be used to find substrings, the function returns all the contents of the first occurrence of a substring, the format of the STRSTR () function is as follows: String Strstr (String $haystack, Mixed $needle) in the above syntax, the description of the parameters involved is haystack: Specifies the string to be searched, needle Specifies the string to be searched, and if the argument is a number, it matches the character of the numeric ASCII value. In the actual application, there will be ignoring the case of letters, this time, you can use PHP provides a case-insensitive lookup function--stristr () function, the use of this function is the same as the use of the STRSTR () function.
Find the location of the string: The function of the Strpos () function is similar to the STRSTR () function, except that the return is not a string, but rather the first occurrence of a string in another string, the syntax of the Strpos () is as follows: Int Strpos (String $ Haystack,mixed $needle [, int $offset = 0]) The parameters involved in the above syntax are described as follows: Haystack is the string to be searched, needle Specifies the string to find, offset is the location where you want to start the search, The default value is 0.
Strpos () function is a case-sensitive lookup function, but in the actual application process will often appear to ignore the case, this time you can use PHP to provide a case-insensitive lookup function Stripos (), the function of the use of the method and Strpos () is the same.
- Comparing strings
In the PHP language, comparing the size of two strings can be done in two ways: by using the "= =" operator and comparing it with a function
Use the "= =" operator to compare the size of two strings: the easiest way to compare two strings in PHP is to use the double equals operator (= =).
Use the function to compare the size of the string: the strcmp () function in PHP provides a more accurate comparison of the size of two strings, with the syntax formatted as int strcmp (string $str 1,string $str 2) The parameters involved in the above syntax are described as follows: str1 specifies the string to be compared 1,STR2 specifies the string 2 to be compared. This strcmp familiar can ensure that two strings match exactly, and return the comparison as an integer, the return value of this function has the following three kinds. 0: Two strings are equal, less than 0, the first string is less than the following string, if the return value is greater than 0, then the preceding string is greater than the following string.
In addition to the strcmp () function, PHP provides other similar comparison functions, such as the strncmp () function to select the length (number of characters) of the string you want to compare, with the syntax in the following format: int strcmp (string $str 1,string $ Str2,int $len) The above parameter description str1: Specifies the first string to compare, str2: Specifies the second string to compare Len: Specifies the number of characters each string is used to compare.
When comparing strings, sometimes you need to ignore the case, you can use the Strcasemp () function and the strncasemp () function, these two functions are identical to the case-sensitive function, the strcasecmp () function and strncasecmp () The syntax format for the two functions is as follows:
int strcasecmp (String $str 1,string $str 2)
int strncasecmp (string $str 1,string $str 2,int $len)
- Copy string
If you need to repeat n times the function of a character or a string, the simplest implementation is to call the copy function, in PHP you can use the Str_repeat () function to implement the copy of the string, the syntax of the function is as follows: String Str_repeat (String $input, int $multiplier) The description of the parameter that is designed in the above syntax is as follows input specifies the string to repeat, multiplier specifies the number of times the string will be repeated.
- Flip string
Working with strings also includes flipping strings in the PHP language, using the Strrev () function to implement the ability to invert strings, the Strrev () function has a syntax format of string Strrev (string $string)
The above parameter, string, is used to specify the string to flip.
- Slicing, merging strings
Splitting a string into multiple strings by some rule, or merging multiple strings into a long string, is a common problem when dealing with string manipulation. Using the explode () function provided by PHP, the Str_split () function, the implode () function can handle problems like slicing and merging strings.
Slice string: The function of the explode () function is to divide the string into an array with the specified delimiter, and the syntax 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: Specifies where to separate strings, string: Specifies the string to be split, limit: Specifies the maximum number of array elements returned, and the last child block will contain the remainder of the string.
The Str_split () function is capable of splitting a string into multiple substrings of equal length. The syntax format for the Str_split () function is as follows: Array str_split (string $string [, int $split _length=1]) The parameters in the syntax above are described as follows: string: Specifies the string to split, Split_ Length: Specifies how long each array element is, and the default value is "1".
Merging strings: The function of the implode () function is to concatenate the elements of an array into a string, the syntax of the implode () function is as follows: String implode ([string $glue],array $pieces) The parameters involved in the above syntax are described as follows: Glue () Specifies what is placed between the elements of the array, the default value is "" (representing a space string) pieces specifies the array to be merged into a string. Call the implode () function to obtain a new string based on the conditions that the parameter restricts, to achieve the purpose of merging strings.
Join () is an alias of the implode () function, and the usage of the two functions is exactly the same, and it should be emphasized that although parameter glue is optional, two parameters are recommended for better compatibility of the program.