This article illustrates the substitution, segmentation, and connection methods of PHP strings. Share to everyone for your reference, specific as follows:
Substitution of strings
1. Perform search and replace of a regular expression
Copy Code code as follows:
Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])
Searches the part of the subject that matches the pattern and replaces it with replacement.
2. Substring substitution
Copy Code code as follows:
Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])
The function returns a string or array. The string or array is the result of replacing all search in subject with replace.
Segmentation and concatenation 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-use a string to split another string
Description
Array explode (string $separator, string $string [, int $limit])
$str = ' One|two|three|four ';
Positive limit
print_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] = > Two
[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
For more information about PHP interested readers can view the site topics: "PHP array Operation skills Encyclopedia", "PHP Data structure and algorithm tutorial", "PHP Mathematical Operation Skills Summary", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.