Home > Backend Development > PHP Tutorial > scanf string string operation function commonly used in PHP development

scanf string string operation function commonly used in PHP development

PHP中文网
Release: 2016-07-29 08:44:22
Original
1385 people have browsed it

            1. Splicing strings
Splicing strings is one of the most commonly used string operations. PHP supports three ways to splice strings, namely dots, delimiter {} operations, and dots The equal sign .= can be used to perform operations. The dot equal sign can decompose a relatively long string into several lines for definition, which is more beneficial.
2. Replace strings
In the language PHP, it is provided A function named substr_replace() is created. The function of this function can quickly complete the string replacement function of scanning and editing text with a large content. Its syntax format:
mixed substr_replace(mixed $string, string $replacement, int $ start[,int $length])
Explanation of the above syntax format:
string is the string to be checked or to be replaced
replacement specifies the string to be inserted or replaced
start specifies where in the string Start replacement. This parameter can take three types of values ​​(positive number: replace from the start offset at the beginning of the string, negative number, start replacement 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 three types of values ​​(positive number: the length of the string to be replaced, negative number: from the end of the string The length of the string to be replaced at the beginning, 0: it is an insertion operation, not a 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 the string length and returns the length information of the string. The format of this syntax is as follows: int strlen(string $string) The string in the format is used to specify the string whose length is to be calculated.
2) Calculate the number of strings
In PHP , The substr_count() function can be used to determine very conveniently and accurately how many specified substrings there are in the provided string. The syntax format of the substr_count() function is as follows: int substr_count(string $haystack,string $ needle[,int $offset=0[,int $length]]) The description of the parameters designed in the above syntax is 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 very conveniently and accurately determine the number of characters provided for a certain word How many are there in the 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 above syntax are described as follows: string Used to specify the string to be checked, format is used to specify the return value of the str_word_count() function. The return value of this parameter can return three values, namely 0, 1, and 2. Returning 0 means the default value, Returns the number of words found. If the return value is 1, then str_word_count() returns an array in which the keys are consecutive integers starting from 0 and the values ​​are actual words. If the value of format is 3 , then the return value of the str_word_count() function is an array, in which the key of the array is the position of the word in the string, and the value is the actual word.
4. Searching for strings
String search can be divided into many types , such as searching for substrings, finding the position of a certain string, etc., PHP provides corresponding functions for each string search operation.
1) Searching for substrings
In PHP language, strstr() function It can be used to find substrings. The result returned by this function is all the content 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 description of the parameters involved is as follows haystack: specifies the string to be searched, and needle specifies the string to be searched. If the parameter is a number, it will match the character of the ASCII value of the number. In actual applications, it will When you want to ignore the case of letters, you can use the stristr() function, a case-insensitive search function provided by PHP. The use of this function is the same as the strstr() function.
2 ) Find the position of a string
The function of the strpos() function is similar to the strstr() function, except that it returns not a string, but the position where a string first appears in another string, strpos() The syntax format is as follows: int strpos(string $haystack, mixed $needle[,int $offset =0]). The description of the parameters involved in the above syntax is as follows: haystack is the string to be searched, and needle is specified to be searched. The search string, offset specifies the position to start searching, and the default value is 0. The
strpos() function is a case-sensitive search function, but in actual application processes, it is often necessary to ignore case. You can use stripos(), a case-insensitive search function provided by PHP. The usage of this function is the same as strpos().
5. Compare strings
In PHP language, compare two strings The size can be achieved in two ways: using the "==" operator to compare and using functions to compare
1) Using the "==" operator to compare the size of two strings
Comparing the size of two strings in PHP Sometimes, the easiest way is to use the double equal sign operator (==).
2) Use a function to compare the size of strings. The strcmp() function provided in PHP can more accurately compare the sizes of two strings. The syntax format is as follows int strcmp(string $str1, string $str2) is involved in the above syntax. The description of the obtained parameters is as follows: str1 specifies the string 1 to be compared, str2 specifies the string 2 to be compared. This strcmp function can ensure whether the two strings completely match and return the comparison result in the form of an integer. There are three types of return values ​​​​of this function. 0: The two strings are equal. When it is less than 0, the first string is smaller than the following string. If the return value is greater than zero, then it means, The previous string is larger than the following string.
In addition to the strcmp() function, PHP also provides some other similar comparison functions. For example, the strncmp() function can select the length (characters) of the string you want to compare. number), the syntax format is as follows: int strcmp(string $str1, string $str2, int $len) The above parameter description str1: specifies the first string to be compared, str2: specifies the second character to be compared String len: Specifies the number of characters used for comparison in each string.
When comparing strings, sometimes you need to ignore case, so you can use the strcasemp() function and strncasemp() function. These two functions are similar to The usage of the corresponding case-sensitive functions is exactly the same. The syntax formats of the strcasecmp() function and strncasecmp() are as follows:
int strcasecmp(string $str1,string $str2)
int strncasecmp(string $str1 , string $str2, int $len)
6. Copy a string
If you need to display a character or a string repeatedly n times, the simplest way to implement it is to call the copy function. In PHP, you can use str_repeat() The function realizes the copying of strings. The syntax format of this function is as follows: string str_repeat(string $input, int $multiplier). The description of the parameters designed in the above syntax is as follows: input specifies the string to be repeated, and multiplier specifies the string to be repeated. The number of repetitions.
7, Flip the string
The operation of processing the string also includes flipping the string. In the PHP language, the function of reversing the string can be realized by using the strrev() function. The syntax format of the strrev() function is:
string strrev(string $string)
The above parameter string is used to specify the string to be flipped.
8. Split and merge strings
Cut a string into multiple strings according to certain rules, or Merging multiple strings into one long string is a problem that is often encountered when processing string operations. Use the explode() function, str_split() function, and implode() function provided by PHP to handle similar operations. Problems with splitting and merging strings.
1) Split strings
The function of the explode() function is to split the string into an array using the specified delimiter. 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 explained as follows: delimiter: specifies where to separate the string, string: specifies the string to be split, limit: specifies the returned array The maximum number of elements, the last sub-block will contain the remaining part of the string. The function of str_split() function is to split a string into multiple substrings of equal length. The syntax format of str_split() function is as follows: array str_split(string $string[,int $split_length=1]) The parameters in the above syntax are explained as follows: string: specifies the string to be split, split_length: specifies the length of each array element, the default value is "1".
2) Combine strings
The function of the implode() function is to connect the elements of the array into a string. The syntax format of the implode() function is as follows: string implode([string $glue], array $pieces) In the above syntax The description of the parameters involved is as follows: glue() specifies the content placed between the array elements. The default value is "" (representing a space string). pieces specifies the array to be merged into a string. The implode() function can be called according to the parameters. Obtain a new string based on restricted conditions to achieve the purpose of merging strings.
join() is an alias of the implode() function. The usage of the two functions is exactly the same. It needs to be emphasized that although the parameter glue is optional , but in order to make the program have better compatibility, it is recommended to use two parameters.
Summarize the related operation methods of strings:
1) printf() function: You can format strings
2) sprintf( ) function: You can format a string. The difference from the printf() function is that you need to use echo to display and output the formatted string.
3) nl2br() function: You can convert the newline character in the string "n" is converted to "
" in HTML and displayed.
4) wordwrap() function: Specifies forced line wrapping from a certain column of characters
5) strtolower() function: The function of this function is to convert the characters in the string All characters are converted to lowercase characters
6) strtoupper() function: The function of this function is to convert all characters in the string to uppercase characters
7) ucwords() function: The function of this function is to convert the characters All the first characters in the string are converted to uppercase characters
8) substr_replace() function: can quickly scan and edit strings with large text content
9) strlen() function: can calculate the length of the string and return The length of the string
10) substr_count() function: You can determine how many times a string is in the provided string
11) str_word_count() function, you can determine the number of occurrences of a certain word in a string.
12) strstr() function , can be used to find substrings. The return result of this function is all the content after the first occurrence of the substring
13) strpos() function, the function is similar to the strstr() function, except that it returns not a string , but the position where one string first appears in another string.
14) strcmp() function, this function can accurately compare the sizes of two strings
15) strncmp() function, you can Select the length (number of characters) of the string to be compared
16) strcasecmp() function, you can compare two strings while ignoring case
17) strncasecmp() function, you can ignore case Under the premise, the length or number of characters is compared between two strings.
18) str_repeat() function, which repeats the function of displaying a character or string n times
19) strrev() function, which provides the function of flipping strings Operation
20) The explode() function provides the function of splitting a string into multiple strings. Specify the delimiter to split the string into an array.
21) The str_split() function can split a string into Split into multiple substrings of equal length
22) implode() function, connect the elements of the array to form a string
23) join() function, the usage method is exactly the same as the implode() function, its function It also connects the array elements to form a string

The above introduces the scanf string string operation function commonly used in PHP development, including the content of scanf string. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template