Php string replacement, segmentation and connection methods, php string replacement and segmentation. Php string replacement, segmentation, and connection methods. php string replacement and segmentation this article describes how to replace, split, and connect php strings. For your reference, such as replacing, splitting, and connecting php strings, and replacing and splitting php strings.
This article describes how to replace, split, and connect php strings. We will share this with you for your reference. The details are as follows:
String replacement
1. search and replace a regular expression
The code is as follows: mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])
Search for the part in the subject that matches pattern and replace it with replacement.
2. substring replacement
The code is as follows: mixed str_replace (mixed $ search, mixed $ replace, mixed $ subject [, int & $ count])
This function returns a string or array. This string or array is the result after all the searches in the subject are replaced by replace.
String segmentation and connection
Use a regular expression to separate strings
Description
1. array preg_split (string $ pattern, string $ subject [, int $ limit =-1 [, int $ flags = 0])
Use a regular expression to separate a given string.
2. explode-use one string to separate another string
Note:
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 routine will output:
Array( [0] => one [1] => two|three|four)Array( [0] => one [1] => two [2] => three)
3. string implode (string glue, array pieces) ---- the connected array is called a string.
$lan=array("a","b","c");implode("+", $lan);//a+b+c