PHP string substitution, segmentation and connection method graphic

Source: Internet
Author: User
This article mainly introduced the PHP string substitution, the partition and the connection method, analyzed the function and the use method of Preg_replace, Str_replace, preg_split, explode and implode, the need friend can refer to the following

Substitution of strings

1. Perform a search and replace of a regular expression

The code is as follows:

Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])

Searches for the part of the subject that matches the pattern, replacing it with replacement.

2. Sub-string substitution

The code is as follows:

Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])

The function returns a string or an array. The string or array is the result of replacing all search in subject with replace.

Splitting and linking 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-using one string to split another string

Description

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

$str = ' one|two|three|four ';//Positive Limitprint_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] = [  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

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.