scanf string manipulation functions commonly used in PHP development

Source: Internet
Author: User
Tags first string php language strcmp
1, stitching strings
The concatenation of strings is one of the most commonly used string operations, and in PHP there are 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, this is more advantageous.
2, replacing the 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 grammatical format:
Mixed Substr_replace (mixed $string, string $replacement, int $start [, int $length])
For a description of the syntax format above:
String is the character to be checked or to be replaced
replacement specifies the string to be used for inserting or replacing
Start specifies where the string is to begin replacing, and this parameter can take three types of values (positive number: Starting with the start offset from the beginning of the string, negative numbers, starting with the start offset at the end of the string, 0: Starting with the first character in a string)
length specifies how many characters to replace, this parameter can also take three types of values (positive: the length of the substituted string, negative number: The length of the string to be replaced from the end of the string, 0: The insertion operation, not the replacement operation);
3, calculating the string
1) Calculate the length of the string
In PHP, the strlen () function is used to calculate the length of a string and to return the length information for that string. The syntax is formatted as follows: The string in the format int strlen (string $string) is used to specify the strings to be computed for length.
2) 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 be checked, needle is used to specify the string to be inserted. 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 return value, the return value of this parameter can return three values, respectively, is 0,1,2. Where return 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, 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.
4, finding the 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.
1) Look up substrings
In the PHP language, the STRSTR () function can be used to find substrings, and the function returns all the contents of the first occurrence of a substring, the strstr () function is formatted 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, needle Specifies the string to be searched, and if the argument is a number, it matches the character of the numeric ASCII value. In Practical applications, Occurs when you want to ignore 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.
2) 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 within another string, and 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 look for, offset is the location to start the search, and 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.
5, 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
1) Compare the size of two strings using the "= =" operator
The easiest way to compare two strings in PHP is to use the double equals operator (= =).
2) Use a function to compare the size of a string
The strcmp () function in PHP provides a more accurate comparison of the size of two strings, with the following syntax: int strcmp (String $str 1,string $str 2) The parameters involved in the above syntax are described below. STR1 specifies the string to compare 1 A str2 that specifies the string to compare 2. This strcmp is familiar with ensuring that two strings match exactly, and returns the comparison as an integer, with the following three return values. 0: Two strings are equal, less than 0, and the first string is less than the subsequent string. If the return value is greater than 0, then the preceding string is greater than the subsequent 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)
6, 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.
7, flip string
The manipulation of strings also includes flipping strings in the PHP language, using the Strrev () function to implement the function of reversing strings, the syntax of the Strrev () function is
String Strrev (String $string)
The above parameter, string, is used to specify the string to flip.
8, slicing, merging strings
Splitting a string into multiple strings according to a certain rule, or merging multiple strings into a long string, is a common problem when dealing with string manipulation. Use the explode () function provided by PHP, the Str_split () function, implode () The function can handle problems like slicing, merging strings.
1) Slicing strings
The function of the explode () function is to cut the string into an array with the specified delimiter, and the explode () function has the following syntax: Array explode (string $delimiter, string $string [, int $limit]) The parameters involved in the above syntax are described as follows: delimiter: Specifies where the string is delimited, 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 Str_split () function has the following syntax format: Array str_split (string $string [, int $split _length=1]) The parameters 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 a string, and the syntax of the implode () function is as follows: String implode ([string $glue],array $pieces) The arguments involved in the syntax above are described as follows: Glue () Specifies what is placed between the elements of the array, with the default value of "" (representing a space string) pieces specifies the array to be merged into a string. Calling the implode () function gets 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.
Summarize how the string is Related:
1) printf () function: You can format a string
2) sprintf () function: You can format the string, and the difference between the printf () function is that you need to use Echo to display the formatted string output.
3) NL2BR () function: You can convert a line break in a string "\ n" to "
"And then show it.
4) WordWrap () function: Specifies to force a line break from a column character
5) Strtolower () function: This function implements the function of converting all characters in a string to lowercase letters
6) Strtoupper () function: The function is to convert all characters in a string to uppercase characters
7) Ucwords () function: The function is to convert all the first characters in the string to uppercase letters
8) Substr_replace () function: the ability to quickly scan and edit strings with more text content
9) strlen () function: You can calculate the length of a string and return the length of the string
Substr_count () function: You can determine how many characters a string has in the supplied string
One) the Str_word_count () function, which determines the number of occurrences of a word in a string.
The Strstr () function, which can be used to find a substring, returns the result of all the contents of the first occurrence of a substring
The Strpos () function, the function and the STRSTR () function are similar, except that the return is not a string, but rather the position of the first occurrence of a string in another string.
strcmp () function, the function can accurately compare the size of two strings
STRNCMP () function, you can select the length of the string to compare (the number of characters)
STRCASECMP () function, you can compare two strings when ignoring the case
STRNCASECMP () function, you can compare the length or number of characters to two strings when ignoring the case.
Str_repeat () function that repeats n times the function of displaying a character or string
Strrev () function, which provides an operation to flip a string
The explode () function, which provides the ability to slice a string into multiple strings, specifies that the delimiter string is cut into an array.
Str_split () function, the ability to split a string into multiple substrings of equal length
Implode () function to concatenate the elements of an array into a string.
) join () function, which is used in the same way as the implode () function, which is also the function of connecting the elements of an array into a string.

The above describes the scanf string PHP development commonly used in string manipulation functions, including the contents of the scanf string, I hope that the PHP tutorial interested in a friend helpful.

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