Home > Backend Development > PHP Tutorial > 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

墨辰丷
Release: 2023-03-29 12:40:01
Original
1784 people have browsed it

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 ]] )
Copy after login

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 ] )
Copy after login

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));
Copy after login

The above routine will output:

Array
(
  [0] => one
  [1] => two|three|four
)
Array
(
  [0] => one
  [1] => two
  [2] => three
)
Copy after login

3. string implode(string glue, array pieces) ——— — The connected array is called a string

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template