1, stitching string
The concatenation string is one of the most commonly used string operations and supports three ways to stitch strings in a PHP tutorial. is a dot. Delimiter {} operation, there is also a dot equals. = to operate, the dot equals sign can decompose a longer string into several lines to define, which is more beneficial.
2, replacement string
In the language of PHP, a function named Substr_replace () is provided, which can quickly scan and edit the string substitution function of the text content. His grammatical format:
Mixed Substr_replace (mixed $string, string $replacement, int $start [, int $length])
For a description of the above syntax format:
String to be checked or to be replaced
replacement Specifies the string you want to insert or replace
Start specifies where the string is to be replaced, this parameter can take three types of values (positive numbers: The start offset from the beginning of the string, negative numbers, starting at the beginning of the end of the string, 0: Replacing from the first character in the string)
length specifies how many characters to replace, which can also take three types of values (positive numbers: length of the replaced string, negative numbers: The length of the string to be replaced from the end of the string, 0: The insert operation, not the substitution operation);
3, calculating the string
1 Calculate the length of the string
In PHP, the strlen () function computes the length of the string and returns the length information of the string. The syntax is formatted as follows: the string in the int strlen (string $string) format is the one used to specify the length to be computed.
2 count the number of strings
In PHP, the Substr_count () function can be used very conveniently and accurately to determine how many of a given substring is in the supplied string, and the syntax for 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 below: haystack Specifies the string to be checked, needle to specify the string to insert, Offset is used to specify where the search is to begin 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 easily and accurately determine how many words a word has in the supplied string, and the syntax format of the Str_word_count () function is as follows: Mixed Str_word_count ( string $string [, int $format =0[,string $charlist]] The arguments that are involved in the syntax above are described as follows: string specifies the string to be checked, and format is used to specify Str_word_count () The return value of the function, which returns a value of three values, which is 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. The key name is a sequential integer starting with 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 in which the key name of the array is the position of the word in the string, and the value is the actual word.
4, find the string
The search for strings can be divided into a number of ways, such as finding substrings, finding the location of a string, and so on, and PHP provides the corresponding function for each string lookup operation.
1) Lookup substring
In the PHP language, the STRSTR () function can be used to find a substring that returns the results of all the contents of the first occurrence of the substring, and the strstr () function is in the following format: String Strstr (String $haystack, mixed $ Needle) in the above syntax, the parameters involved are described as follows haystack: Specifies the searched string, needle specifies the string to search for, and if the argument is a number, it matches the character of the numeric ASCII value. In Practical applications, There will be cases where you want to ignore the case of letters, and you can use a case-insensitive lookup function--stristr () function provided by PHP, using the same method as the Strstr () function.
2 Find the location of the string
The function of the Strpos () function is similar to the STRSTR () function, except that the string is not returned but rather the first occurrence of a string within another string, the syntax of the Strpos () is as follows: int Strpos (String $haystack, Mixed $needle [, int $offset = 0]) The arguments 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 the search is to be started, and the default value is 0.
The Strpos () function is a case sensitive lookup function, but in the actual application process there will often be a need to ignore the case, this time you can use a PHP provided by the case is not very sensitive lookup function Stripos (), the function of the use of methods and Strpos () is the same.
5, comparing strings
In the PHP language, comparing the size of two strings can be done in two ways: using the "= =" operator to compare and use functions for comparison
1 using 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 (= =).
2 using a function to compare the size of a string
The strcmp () function provided in PHP provides a more accurate comparison of the size of the two strings, with the following syntax format 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 is well versed in ensuring that two strings match exactly and returns the comparison as an integer, with three of the return values of the function. 0: Two strings are equal and 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 larger than the following string.
In addition to the strcmp () function, PHP provides other comparable comparison functions, such as the strncmp () function to select the length of the string you want to compare (the number of characters), the syntax is as follows: int strcmp (string $str 1,string $ Str2,int $len) Description str1: Specifies the first string to compare, str2: Specifies the second string len to compare: Specifies the number of characters each string is used for comparison.
When comparing strings, sometimes you need to ignore the case, you can use the Strcasemp () function and the strncasemp () function, which is exactly the same as the use of case-sensitive corresponding functions, strcasecmp () functions and STRNCASECMP () The syntax format of the two functions is as follows:
int strcasecmp (String $str 1,string $str 2)
int strncasecmp (string $str 1,string $str 2,int $len)
6, copy string
If you need to repeat the function of a character or a string, the simplest way to do this is to invoke the copy function, which in PHP can be used to replicate the string using the Str_repeat () function, which has the following syntax format: String Str_repeat (String $input, int $multiplier) Description of the parameters designed in the syntax described below specifies the string to repeat, multiplier the number of times the specified string will be repeated.
7, Flip the string
Manipulating strings also includes flipping strings in the PHP language, the Strrev () function can be used to reverse the function of the string, the Strrev () function is syntactically formatted as
String Strrev (String $string)
The argument string above is used to specify the string to flip.
8, splitting, merging strings
Splitting a string into multiple strings according to a rule, or merging multiple strings into a long string, is a frequent problem when dealing with string operations. Use the explode () function provided by PHP, Str_split () function, implode () Functions can handle problems like splitting and merging strings.
1) Segmentation string
The function of the explode () function is to cut 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 arguments involved in the above syntax are described as follows: delimiter: Specifies where to delimit the string, 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 the ability to split 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 arguments in the above syntax are described as follows: string: Specifies the string to be split, Split_length: Specifies the length of each array element, and the default value is "1".
2) Merging strings
The function of the implode () function is to concatenate the elements of an array into strings, and the syntax of the implode () function is as follows: String implode ([string $glue],array $pieces) The arguments that are involved in the syntax above are described below: Glue () Specifies the content to be placed between the array elements, with the default value "" (for the space string) pieces specifies the array to be merged into a string. The call to the implode () function gets a new string that is the object of the merged string, depending on the condition that the parameter restricts.
Join () is the alias of the Implode () function, and the use of 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.
Summary of how strings are related: