Common string operation functions in PHP Development, php development string function _ PHP Tutorial

Source: Internet
Author: User
Common string operation functions in PHP development, and string functions in php development. Common string operation functions in PHP Development. string function 1 in php Development. concatenating string concatenation strings is one of the most common string operations, in PHP, three methods are supported to operate string functions commonly used in PHP Development. in php, string functions are developed.

1. concatenate strings
Concatenating strings is one of the most common string operations. in PHP, you can concatenate strings in three ways: dots. separator {} operation, 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.

2. replace the 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])
The preceding syntax format is described as follows:
String is the string to be checked or to be replaced
Replacement specifies the string to be inserted or replaced
Start specifies where to start replacement of the string. this parameter can be set to three types of values (positive number: starting from the start offset at the beginning of the string, negative number, starting from the start offset at the end of the string, 0: starting from the first character in the string)
Length specifies the number of characters to be replaced. this parameter can also be set to three types of values (positive number: length of the string to be replaced, negative number: the length of the string to be replaced from the end of the string. 0 indicates the insert operation, not the 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 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.

2) 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. the syntax format of the substr_count () function is as follows: int substr_count (string $ haystack, string $ needle [, int $ offset = 0 [, int $ length]) the parameters designed in the preceding syntax are described as follows: haystack specifies the string to be checked. needle is used to specify the string to be inserted. offset is used to specify where to start searching in the string. the default value is 0. length is used to specify the search length.
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, 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.

4. 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.

1) search for substrings
In PHP, the strstr () function can be used to search for a substring. the result returned by this function is all the content of the substring that appears for the first time. 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 character 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.
2) locate the string
The strpos () function is similar to the strstr () function, but returns not a string, but the position where a string appears for the first time in another string. strpos () syntax format for example: int strpos (string $ haystack, mixed $ needle [, int $ offset = 0]). the parameters involved in the preceding syntax are described 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.

5. compare strings
In PHP, comparing the size of two strings can be achieved in two ways: using the "=" operator to compare and using functions

1) 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 (= ).
2) use a function to compare the string size
The strcmp () function provided in PHP can compare the size of two strings more accurately. 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 to be compared 1, str2 specifies the string to be compared 2. 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 are as follows. 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 a string to be greater than that of a string ), 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)

6. copy the 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 str_repeat () the function implements string reproduction. 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, and multiplier specifies the number of times the string will be repeated.

7. flip the string
The operations to process strings include turning the strings into PHP. the strrev () function can be used to reverse the string. the syntax format of strrev () function is
String strrev (string $ string)
The string parameter above is used to specify the string to be flipped.

8. 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.

1) split the string
The function of the explode () function is to split the string into arrays using the specified delimiter. the syntax format of The explode () function is as follows: array explode (string $ delimiter, string $ string [, int $ limit]) the parameters involved in the preceding syntax are described as follows: delimiter: specifies the separator string, string: specifies the string to be split, 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 ".
2) merge strings
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 to be placed between array elements. the default value is "" (indicating a space string) pieces specifies the array to be merged as a string. 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.
To sum up the operation methods of the string:

1) printf () function: you can format strings.
2) sprintf () function: you can format a string. The difference between sprintf () and printf () is that you need to use echo to display and output formatted strings.
3) nl2br () function: you can convert the line break "\ n" in the string to "in HTML"
.
4) wordwrap () function: specifies to force line feed from a column of characters
5) strtolower () function: This function converts all characters in a string to lowercase characters.
6) strtoupper () function: This function converts all the characters in the string to uppercase characters.
7) ucwords () function: converts all the first characters in a string to uppercase characters.
8) substr_replace () function: This function allows you to quickly scan and edit strings with more text content.
9) strlen () function: calculates the length of a string and returns the length of the string.
10) substr_count () function: determines the number of characters in a string provided.
11) the str_word_count () function determines the number of times a word appears in a string.
12) strstr () function, which can be used to search for substrings. the result returned by this function is all the content after the first occurrence of the substrings.
13) the strpos () function is similar to the strstr () function, but returns the position of a string in another string rather than a string.
14) strcmp () function, which can accurately compare the size of two strings
15) strncmp () function. you can select the length (number of characters) of the string to be compared)
16) strcasecmp () function, which can be used to compare two strings when case sensitivity is ignored.
17) the strncasecmp () function can compare the length or number of characters of two strings without case sensitivity.
18) str_repeat () function, which repeatedly displays one character or string n times
19) strrev () function, which allows you to flip strings.
20) The explode () function allows you to split a string into multiple strings. specify the delimiter to split the string into arrays.
21) str_split () function, which can split a string into multiple sub-strings of equal length
22) The implode () function connects the elements of the array to a string.
23) the join () function uses the same method as the implode () function. its function is to concatenate array elements into a string.

Concatenation 1: concatenate a string to concatenate a string is one of the most common string operations. in PHP, three methods are supported to concatenate strings...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.