Home  >  Article  >  Backend Development  >  Detailed graphic explanation of the replacement, splitting and joining methods of PHP strings

Detailed graphic explanation of the replacement, splitting and joining methods of PHP strings

墨辰丷
墨辰丷Original
2018-06-02 10:20:191756browse

This article mainly introduces the replacement, splitting and connecting methods of PHP strings, and analyzes the functions and usage of functions such as preg_replace, str_replace, preg_split, explode and implode. Friends in need can refer to it

String replacement

1. Perform a regular expression search and replace

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 the pattern and replace it with replacement.

2. Substring replacement

Code 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

Instructions

1. array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

Through a regular expression Separate the given string with the formula.

2. explode — Use one string to split another string

Instructions:

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 a string

$lan=array("a","b","c");
implode("+", $lan);//a+b+c

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

PHP uses the PDO abstraction layer to obtain query results

PHP remembers the state when implementing a search Method example

PHP method example to compress image size and convert it to jpg format

The above is the detailed content of Detailed graphic explanation of the replacement, splitting and joining methods of PHP strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn