This article mainly introduced the PHP string substitution, the partition and the connection method, analyzed the function and the use method of Preg_replace, Str_replace, preg_split, explode and implode, the need friend can refer to the following
Substitution of strings
1. Perform a search and replace of a regular expression
The code is as follows:
Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])
Searches for the part of the subject that matches the pattern, replacing it with replacement.
2. Sub-string substitution
The code is as follows:
Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
The function returns a string or an array. The string or array is the result of replacing all search in subject with replace.
Splitting and linking of strings
Separating strings with a regular expression
Description
1. Array Preg_split (string $pattern, string $subject [, int $limit =-1 [, int $flags = 0]])
Separates the given string by a regular expression.
2. explode-using one string to split another string
Description
Array explode (string $separator, string $string [, int $limit])
$str = ' one|two|three|four ';//Positive Limitprint_r (Explode (' | ', $STR, 2));//negative limit (from PHP 5.1) Print_r (Explode (' | '), $STR, -1));
The above routines will output:
Array ( [0] = one [1] = Two|three|four) Array ( [0] = one [1] = [ 2] = three)
3. String implode (string glue, array pieces) ———— connection array is called a string
$lan =array ("A", "B", "C"), Implode ("+", $lan);//a+b+c
Summary: The above is the entire content of this article, I hope to be able to help you learn.