SoFunction
Updated on 2025-03-09

Replacement, segmentation and connection methods of php strings

This article describes the replacement, segmentation and connection methods of php strings. Share it for your reference, as follows:

String replacement

1. Perform a regular expression search and replacement

Copy the codeThe 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

Copy the codeThe 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 searches in the subject by replace.

Split and concatenation of strings

Separate strings by a regular expression

illustrate

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

Separate the given string by a regular expression.

2. exploit — Split another string using one string

illustrate:

array explode ( string $separator , string $string [, int $limit ] )

$str = 'one|two|three|four';
// Positive limitprint_r(explode('|', $str, 2));
// Negative limit (starting 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 concatenation array is called a string

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

For more information about PHP related content, please check out the topic of this site:Complete collection of PHP array (Array) operation techniques》、《PHP data structure and algorithm tutorial》、《Summary of PHP mathematical operation skills》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.