PHP, String (ii)

Source: Internet
Author: User
Tags explode

Tag: string requires size lookup byte start ACK failure starts

1. Comparing strings

(1) Comparison of strings by byte

int strcmp (String str1,string str2): Parameter str and parameter str2 specify the two strings to compare. If the function returns 0, the function returns a value greater than 0 if the parameter str1 is greater than the parameter str2, or the function returns less than 0 if the parameter str1 is less than the str2 of the parameter; The function is case-sensitive

int strccasemp (String str1,string str2): Parameter str and parameter str2 specify the two strings to compare. If the function returns 0 if it is equal, the function return value is greater than 0 if the parameter str1 is greater than str2, and if the parameter str1 is less than the parameter str2, the function return value is less than 0; the function is not case-sensitive;

(2) Comparison of strings by natural sorting method

int strnatcmp (String str1,string str2): Parameter str and parameter str2 specify the two strings to compare. If the function returns 0 if it is equal, the function return value is greater than 0 if the parameter str1 is greater than str2, and if the parameter str1 is less than the parameter str2, the function return value is less than 0; the function distinguishes the letter case;

(3) Specifying the location of the source string to start the comparison

The strncmp () function is used to compare the first n characters in a string.

int strncmp (string str1,string str2,int len): Parameter str and parameter str2 specify the two strings to compare. If the function returns 0 if it is equal, the function return value is greater than 0 if the parameter str1 is greater than the parameter str2, or if the parameter str1 is less than the str2 of the parameter, the function return value is less than 0;len specifies the number of characters participating in the comparison in each string, which distinguishes the letter case.

2. Retrieving strings

(1) Use the STSRSTR () function to find the specified keyword

Gets a string that specifies the position of the first occurrence of the string in another string at the end of the latter. If the execution succeeds, it returns the obtained string (there is a matching character) and returns False if it fails. The syntax format is as follows:

String Strstr (String haystack,string needle): Haystatck: Specifies the string from which to search; Specifies the object to search for, and if the parameter is a numeric value, the character that matches the ASCII value of the numeric value is searched. This function distinguishes the case of letters.

(2) Use the Substr_count () function to retrieve the number of occurrences of a substring

Gets the number of occurrences of the specified character in the string, in the following syntax format:

int Substr_count (String haystack,string needle)

The parameter haystack is the specified string, and the parameter needle is the specified character.

3. Replacing strings

(1) str_ireplace () function

Replaces the string specified in the original string with the new string, in the following syntax format:

Mixed str_ireplace (mixed search,mixed replace,mixed subject [, int &count])

Replace all parameters that appear in parameter subject with the parameter replace, and the parameter count indicates the number of times to replace the string execution, and this function is not case-sensitive.

Search: Specifies the string to find;

Replace: Specifies the value of the substitution;

Subject: Specifies the scope of the lookup;

Count: Gets the number of times the substitution was performed

(2) Substr_replace () function

Replaces some of the strings in the specified string with the following syntax:

String Substr_replace (String str,string repl,int start,[,int length]):

STR: Specifies the original string to manipulate;

REPL: Specifies the new string after replacement;

Start: Specifies where the replacement string begins.

Length: Specifies the length of the replacement string.

4. Formatting strings

The Number_format () function is used to format a numeric string.

5. Splitting a string

The segmentation of a string is achieved through the explode () function. The explode () function splits a string by the specified rules and returns an array of values. The syntax format is as follows:

Arrary Explode (string separator,string str,[,int limit])

6. Crafting a string

The implode () function can combine the contents of an array into a new string. The syntax format is as follows:

String implode (string glue,string pieces);

The parameter glue is a string type that specifies the delimiter, and the parameter pieces is an array type that specifies the array to be merged.

PHP, String (ii)

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.