Home >Backend Development >PHP Tutorial >PHP string replacement, splitting and concatenation methods, PHP string replacement and splitting_PHP tutorial
The examples in this article describe the replacement, splitting and joining methods of php strings. Share it with everyone for your reference, the details are as follows:
String replacement
1. Perform a regular expression search and replace
Copy code The code is as follows: mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
Search for the part of the subject that matches pattern and replace it with replacement.
2. Substring replacement
Copy code 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 of replacing all search in subject with replace.
Splitting and concatenating strings
Separate strings by 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 — Use one string to split another string
Description:
array explode ( string $separator , string $string [, int $limit ] )
$str = 'one|two|three|four'; // 正数的 limit print_r(explode('|', $str, 2)); // 负数的 limit(自 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 string
$lan=array("a","b","c"); implode("+", $lan);//a+b+c
Readers who are interested in more PHP-related content can check out the special topics of this site: "Complete PHP Array Operation Skills", "PHP Data Structure and Algorithm Tutorial", "Summary of PHP Mathematical Operation Skills", "php Date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.