Several character string replacement functions in php
In php, there are several character replacement functions, such as str_replace, substr_replace, preg_replace, preg_split, and str_split. I will give you a summary of them.
1. str_replace (find, replace, string, count)
Purpose: The str_replace () function uses one string to replace other characters in the string.
Parameter description
Find is required. Specifies the value to be searched.
Replace is required. Specifies the value to replace the value in find.
String is required. Specifies the string to be searched.
Count is optional. A variable that counts the number of replicas.
Example
In this example, we will demonstrate the str_replace () function with array and count variables:
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 |
Supplement: If count is specified, its value is set to the number of times it is replaced.
Ii. substr_replace (string, replacement, start, length)
Purpose: The substr_replace () function replaces one part of the string with another.
Parameter description
String is required. Specifies the string to be checked.
Replacement is required. Specifies the string to be inserted.
Start is required. Specifies where the string starts replacement.
■ Positive number-start replacement at the start offset
■ Negative number-replace the number starting from the start offset at the end of the string
■ 0-start replacement at the first character in the string
Charlist is optional. Specifies the number of characters to replace.
■ Positive number-length of the replaced string
■ Negative number-Number of replaced characters starting from 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 |
3. preg_replace (pattern, replacement, subject, limit =-1, $ count)
Purpose: search and replace a regular expression.
Parameter description
Pattern is required. The mode to be searched.
Replacement is required. String or array to be replaced.
Subject is required. String or array to be replaced.
The number of limit replacements. -1 is infinite
Count: the number of times the replacement is completed, the variable
Example #1 use backward reference followed by the original numerical value
The Code is as follows: |
Copy code |
$ String = '0000l 15,200 3 '; $ Pattern = '/(w +) (d +), (d +)/I '; $ Replacement = '$ {1} 1, $3 '; Echo preg_replace ($ pattern, $ replacement, $ string ); ?> |
The above routine will output:
April1, 2003
Example #2 preg_replace () using an index-based array
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 routine will output:
The bear black slow jumped over the lazy dog.
4. preg_split (pattern, subject, limit =-1, flag)
Purpose: Use a regular expression to split a string.
Parameter description
Pattern is required. The mode to be searched.
Replacement is required. String or array to be replaced.
Subject is required. String to be replaced.
The limit string can be separated by up to limit.
Flag Mode
Example 1672. preg_split (): Obtain the search string component
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. Split the 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. Split the string into a match and Its offset
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
)
)
V. str_split (subject, length)
Function: Splits a string into an array.
Parameter description
Subject string.
The 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] => el
[1] => lo
)