String manipulation functions commonly used in PHP development

  • 2020-03-31 21:30:01
  • OfStack

1. Concatenate strings
String concatenation is one of the most commonly used to string manipulation, in PHP support for string concatenation operation, three ways are dots. The delimiter {}, and dot equals. = to operate, dot equals can put a longer string is decomposed into a few lines are defined, doing so is relatively good.

2, replace the string
In PHP, a function named substr_replace() is provided, which can quickly complete the function of scanning and editing the text more than string replacement.
Mixed substr_replace(mixed $string,string $replacement, int $start[,int $length])
For the above syntax format:
A string is a string to be checked or to be replaced
Replacement specifies the string to insert or replace
Start specifies where to start the substitution in the string. This parameter can take three types of values (positive: replace from the start offset at the beginning of the string, negative: replace from the start offset at the end of the string,0: replace from the first character in the string).
Length specifies how many characters to replace. This parameter can also take on three types of values (positive: length of the string to be replaced, negative: length of the string to be replaced from the end of the string,0: insert, not replace).

3. Evaluate the string

1) calculate the length of the string
In PHP, the strlen() function calculates the length of a string and returns the length of that string.

2) calculate the number of strings
In PHP, the substr_count() function can be used to conveniently and accurately determine how many substrings a specified substring has in the supplied string. The syntax of the substr_count() function is as follows :int substr_count(string $haystack,string $needle[,int $offset=0[,int) The parameters designed in the above syntax are specified as follows :haystack specifies the string to check,needle specifies the string to insert,offset specifies where to start the search in the string, default value is 0, and length specifies the length of the search.
In PHP, the str_word_count() function can be used to conveniently and accurately determine how many words are in a given string. The syntax of the str_word_count() function is as follows :mixed str_word_count(string $string[,int $format=0[,string) $charlist]]) in the above parameters involved in the syntax description is as follows: string string to specify the check, the format used to specify the str_word_count () function returns the value, the return value of this parameter can be returned three values, respectively is 0, which returns 0 means the default, returned to find the number of words, if the return value is 1, then the str_word_count () returns is an array, one of the key name consecutive integers from 0, the value is the actual words. If the for When the value of mat is 3, 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.

4. Find the string

There are many ways to find strings, such as finding substrings, finding the location of a string, and so on. PHP provides functions for each string search operation.

1) find substrings
In the PHP language, the STRSTR () function can be used to find a substring. The result of this function is all the contents of the first occurrence of the substring. The format of the STRSTR () function is as follows :string $haystack,mixed $needle) in the syntax of the above, the involved parameters stated below haystack: specify the search string, needle assigned to search string, if this parameter is number, this number will be matchers ASCII value of the characters. In practical applications, appear to ignore letter case, this time, you can use PHP provides a case-insensitive search function - stristr () function, the function of the usage and STRSTR () function is the same.
2) find the position of the string
The strpos() function is similar to the STRSTR () function except that instead of returning a string, it returns the first position of one string in another string. The syntax of strpos() is as follows :int strpos(string $haystack,mixed $needle[,int $offset) Haystack is the string to be searched,needle specifies the string to be searched, and offset is the position to start the search. The default value is 0.
The strpos() function is a case-sensitive lookup function, but in the real world it is often necessary to ignore case. In this case, you can use the stripos() function provided by PHP, which is not very case-sensitive. This function USES the same method as strpos().

5, compare strings
In the PHP language, comparing the size of two strings can be done in two ways: by using the "==" operator and by using functions

1) compare the size of two strings using the "==" operator
The easiest way to compare two strings in PHP is to use the double equal sign operator (==).
2) use functions to compare the size of strings
The STRCMP () function provided in PHP can more accurately compare the size of two strings. The syntax format is as follows: int STRCMP (string $str1,string $str2) Specified to compare strings 1, str2 string specified to compare 2. The STRCMP know well can make sure whether the two strings match completely, and compares the results with returns an integer, the value returned from this function has the following three. 0: two strings are equal, less than zero, the first string is less than the back of the string, if the return value is greater than zero, then, in front of the string is greater than the back of the string.
In addition to STRCMP () function, PHP also offers other similar comparison function, such as STRNCMP () function can choose to want to compare the length of the string (number of characters), the syntax format is as follows: int STRCMP ($str1 string, the string $str2, int $len) shows the parameters above str1: to compare the first string is specified, the str2: specify the second string to compare len: specify each the number of characters in the string used for comparison.
Strcasemp () function and strncasemp() function are the same as their case-sensitive counterparts. The syntax of strcasecmp() function and strncasecmp() function is as follows:
Int strcasecmp ($str1 string, the string $str2)
Int strncasecmp(string $str1,string $str2,int $len)

6. Copy the string
If need to be repeated n times according to the function of a character or a string, then the realization of the simplest way is to call copy function, can be used in the PHP str_repeat () function to realize the copy of the string, the function of syntax format is as follows: string str_repeat ($input string, int $multiplier) in the design parameters in the syntax of the above stated below will repeat input string, multiplier specified string will be repeated.

7. Flip the string
In the PHP language, the strrev() function, which is syntactically formatted as
String strrev (string $string)
The above parameter string is used to specify the string to flip.

8. Shred and merge strings
Splitting a string into strings according to certain rules, or merging multiple strings into a single long string, is a common problem when dealing with string operations.

1) shred the string
The limit () function splits a string into an array with the specified delimiter. The syntax for the limit () function is as follows: string $delimiter,string $string[,int Delimiter: specifies where to delimit strings,string: specifies the string to be split,limit: specifies the maximum number of returned array elements, and the last subblock will contain the rest of the string.
The str_split() function USES the following syntax :array str_split(string $string[,int $split_length=1]).
2) merge strings
The implode() function concatenates the elements of the array into strings. The syntax for the implode() function is as follows :string implode([string $glue],array Glue () specifies what to put between the array elements, and the default value is ""(for a space string)pieces specifies the array to be merged into a string.
Join () is an alias for the implode() function, and the usage of both functions is exactly the same. It is important to note that while the glue parameter is optional, it is recommended to use two parameters for better compatibility.
Summarize the operation methods of strings:

1)printf() function: a string can be formatted
2)sprintf() function: you can format a string. The difference with printf() is that you need to use echo to display the formatted string.
3)nl2br() function: can convert the newline character "\n" in a string to "< "in HTML; Br / >" And then it shows up.
4)wordwrap() function: specifies the character from a column to force a line wrap
5)strtolower() function: this function implements the function of converting all characters in a string to lowercase characters
6)strtoupper() function: this function converts all characters in a string to uppercase characters
7)ucwords() function: this function converts all the first characters in a string to uppercase characters
8)substr_replace() function: the ability to quickly scan and edit strings with more text
9)strlen() function: can calculate the length of a string and return the length of the string
10)substr_count() function: determines how many strings a string has in the supplied string
11) the str_word_count() function, which determines the number of occurrences of a word in a string.
12) the STRSTR () function, which can be used to find a substring, returns everything since the substring first appeared
13) the strpos() function is similar to the STRSTR () function except that instead of returning a string, it returns the position of the first occurrence of one string in another string.
14) STRCMP () function, which can accurately compare the size of two strings
15) STRNCMP () function, you can choose the length of the string to be compared (the number of characters)
16) the strcasecmp() function, which compares two strings while ignoring case
17) the strncasecmp() function, which allows you to compare the length or number of characters of two strings, regardless of case.
18)str_repeat() function that displays a character or string n times
19) the strrev() function, which provides the operation of flipping a string
The limit () function, which provides the ability to slice a string into multiple strings, specifies the delimiter to slice the string into an array.
21) the str_split() function, which can split a string into several substrings of equal length
22)implode() function, which concatenates the elements of an array into strings
23) the join() function, which USES the same script as the implode() function, also concatenates the array elements into a string

Related articles: