Common methods for PHP string handling

Source: Internet
Author: User

Strlen ("string");//Take the length of the string strcmp ("string", "string"); Determines whether two strings are the same, returns 0, differs by 1, and is case-sensitive strcasecmp ("string", "string"); Determine whether two strings are the same, return the same 0, different return 1, case-insensitive Strtolower ("string"); Turn lowercase strtoupper ("string"); Turn capital explode ()   functionDefinition and usage

The explode () function breaks a string into arrays.

Note:the "separator" parameter cannot be an empty string.

Note: This function is binary safe.

Grammar

Explode (separator,string,limit)

Parameters

Describe

Separator

Necessary. Specifies where to split the string.

String

Necessary. The string to split.

Limit

Optional. Specifies the number of array elements that are returned.

Possible values:

  • Greater than 0-returns an array containing a maximum of limit elements
  • Less than 0-returns an array containing all elements except the last -limit element
  • 0-Returns an array containing an element
Implode () function definition and usage

The implode () function returns a string that is composed of array elements.

Note:the implode () function accepts two parameter orders. However, for historical reasons, explode () is not possible, and you must ensure that the separator parameter precedes the string parameter.

Note:The separator parameter of the implode () function is optional. However, for backwards compatibility, it is recommended that you use two parameters.

Note: This function is binary safe.

Grammar

Implode (Separator,array)

Parameters

Describe

Separator

Optional. Specifies what is placed between the elements of the array. The default is "" (an empty string).

Array

Necessary. An array to combine as a string.

Str_replace () function definition and usage

The Substr_replace () function replaces part of a string with another string.

Note: If the start parameter is negative and length is less than or equal to start, length is 0.

Note: This function is binary safe.

Grammar

Substr_replace (string,replacement,start,length)

Parameters

Describe

String

Necessary. Specifies the string to check.

Replacement

Necessary. Specifies the string to insert.

Start

Necessary. Specifies where the string begins to be replaced.

  • Positive number-starts at the specified position in the string
  • Negative number-starts at the specified position from the end of the string
  • 0-Starts at the first character in a string

Length

Optional. Specifies the number of characters to replace. The default is the same length as the string.

  • Positive number-The length of the string being replaced
  • Negative-the number of characters to be replaced starting at the end of the string
  • 0-Insert rather than replace
Str_replace () function definition and usage

The Str_replace () function replaces some characters in a string (case-sensitive).

The function must adhere to the following rules:

    • If the searched string is an array, it returns an array.
    • If the searched string is an array, it will find and replace each element in the group.
    • If you need to find and replace an array at the same time, and you need to execute the replacement element less than the number of elements found, the extra element is replaced with an empty string.
    • If you are looking for an array, but only one string is replaced, the substitution string will work for all found values.

Note: The function is case-sensitive. Use the Str_ireplace () function to perform a case-insensitive search.

Note: This function is binary safe.

Grammar

Str_replace (find,replace,string,count)

Parameters

Describe

Find

Necessary. Specifies the value to find.

Replace

Necessary. Specifies the value that replaces the value in find .

String

Necessary. Specifies the string to be searched.

Count

Optional. A variable that counts the number of replacements.

substr () function definition and usage

The substr () function returns a portion of a string.

Note: If the start parameter is negative and length is less than or equal to start, length is 0.

Grammar

substr (string,start,length)

Parameters

Describe

String

Necessary. A string that specifies the part to return.

Start

Necessary. Specifies where to start the string.

  • Positive number-starts at the specified position in the string
  • Negative number-starts at the specified position from the end of the string
  • 0-Starts at the first character in a string

Length

Optional. Specifies the length of the string to return. The default is until the end of the string.

  • Positive number-returns from the position where the start parameter is located
  • Negative-returns from the end of the string
Str_split () function definition and usage

The Str_split () function splits a string into an array.

Grammar

Str_split (string,length)

Parameters

Describe

String

Necessary. Specifies the string to be split.

Length

Optional. Specifies the length of each array element. The default is 1.

STRISTR () function definition and usage

The Stristr () function searches for the first occurrence of a string in another string.

Note: This function is binary safe.

Note: The function is case-insensitive. For case-sensitive searches, use the strstr () function.

Grammar

Stristr (string,search,before_search)

Parameters

Describe

String

Necessary. Specifies the string to be searched.

Search

Necessary. Specifies the string to search for. If the parameter is a number, the character that matches the ASCII value of the number is searched.

Before_search

Optional. A Boolean value with the default value of "false". If set to "true", it returns the string portion of the first occurrence of the search parameter.

Trim () function definition and usage

The trim () function removes white space characters or other predefined characters on either side of the string.

Related functions:

    • LTrim ()-removes white space characters or other predefined characters to the left of the string.
    • RTrim ()-Removes the white space character or other predefined character to the right of the string.
Grammar

Trim (string,charlist)

Parameters

Describe

String

Necessary. Specifies the string to check.

Charlist

Optional. Specifies which characters are removed from the string. If this argument is omitted, all of the following characters are removed:

  • "+"-NULL
  • "\ T"-tab
  • "\ n"-line break
  • "\X0B"-Vertical tab
  • "\ r"-Enter
  • ""-Space

Common methods for PHP string handling

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.