An explanation of several string substitution functions in PHP

Source: Internet
Author: User

In PHP, there are several character substitution functions, such as: Str_replace, Substr_replace, Preg_replace, Preg_split, Str_split and other functions, let me give you a summary of the introduction.

Yi, Str_replace (find,replace,string,count)

Function: the Str_replace () function replaces some other character in a string with a string.

Parameter description
Find required. Specifies the value to find.
Replace Required. Specifies the value that replaces the value in Find.
string is required. Specifies the string to be searched.
Count is optional. A variable that counts the number of replacements.

Cases

In this example, we will demonstrate the str_replace () function with an array and a count variable:

The code is as follows Copy Code

$arr = Array ("Blue", "Red", "green", "yellow");
Print_r (Str_replace ("Red", "pink", $arr, $i));
echo "Replacements: $i";
?> output:

Array
(
[0] = Blue
[1] = Pink
[2] = Green
[3] = Yellow
)
Replacements:1

Add: If count is specified, its value will be set to the number of times the substitution occurred.

Second, Substr_replace (string,replacement,start,length)

Function: the Substr_replace () function replaces part of a string with another string.

Parameter description
string is required. Specifies the string to check.
Replacement required. Specifies the string to insert.
Start Required. Specifies where the string begins to be replaced.

Positive number-starts with the first offset
Negative number-replaces the start offset starting at the end of a string
0-Start the substitution at the first character in the string

Charlist is optional. Specifies the number of characters to replace.

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

Example

The code is as follows Copy Code

Echo substr_replace ("Hello World", "Earth", 6);
?>

Output:

Hello Earth


Iii. preg_replace (pattern, replacement, Subject,limit =-1, $count)

Function: Perform a search and replace of a regular expression

Parameter description
Pattern required. The pattern that needs to be searched.
Replacement required. The string or array to replace.
Subject required. The string or array to replace.
The number of limit replacements. -1 for unlimited
Number of times that count completed the substitution, variable

Example #1 Use the back reference to follow the value text

The code is as follows Copy Code
$string = ' April 15, 2003 ';
$pattern = '/(w+) (d+), (d+)/I ';
$replacement = ' ${1}1,$3 ';
Echo preg_replace ($pattern, $replacement, $string);
?>

The above routines will output:

april1,2003


Example using an indexed-based array #2 preg_replace ()

The code is as follows Copy Code
$string = ' The quick brown fox jumped over the lazy dog ';
$patterns = Array ();
$patterns [0] = '/quick/';
$patterns [1] = '/brown/';
$patterns [2] = '/fox/';
$replacements = Array ();
$replacements [2] = ' bear ';
$replacements [1] = ' black ';
$replacements [0] = ' slow ';
Echo preg_replace ($patterns, $replacements, $string);
?>

The above routines will output:

The bear black slow jumped over the lazy dog.

Iv. Preg_split (pattern, subject,limit =-1, flag)

Function: Splitting a string with regular expressions

Parameter description
Pattern required. The pattern that needs to be searched.
Replacement required. The string or array to replace.
Subject required. The string to replace.
Limit is divided by the maximum limit of the string.
Flag mode

Example 1672. Preg_split () Example: Get the component of the search string

The code is as follows Copy Code

Split the phrase by any number of commas or space characters,
Which include "", R, T, N and F
$keywords = Preg_split ("/[s,]+/", "Hypertext language, Programming");
?>

Example 1673. Splitting a string into characters

The code is as follows Copy Code

$str = ' string ';
$chars = Preg_split ('//', $STR,-1, preg_split_no_empty);
Print_r ($chars);
?>

Example 1674. Splitting a string into matches and their offsets

The code is as follows Copy Code

$STR = ' hypertext language programming ';
$chars = Preg_split ('//', $STR,-1, preg_split_offset_capture);
Print_r ($chars);
?>

This example will output:

Array
(
[0] = = Array
(
[0] = Hypertext
[1] = 0
)

[1] = = Array
(
[0] = language
[1] = 10
)

[2] = = Array
(
[0] = programming
[1] = 19
)

)

Wu, Str_split (subject,length)

Role: Dividing a string into an array

Parameter description
The subject string.
Length of each segment.

Example 1

The code is as follows Copy Code
Print_r (Str_split ("Hello"));
?>

Output:

Array
(
[0] = H
[1] = = E
[2] = l
[3] = L
[4] = O
)

Example 2

The code is as follows Copy Code
Print_r (Str_split ("Hello", 3));
?>

Output:

Array
(
[0] = Hel
[1] = Lo
)

An explanation of several string substitution functions in PHP

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.