Php string replacement, segmentation and connection methods, php string replacement and segmentation _ PHP Tutorial

Source: Internet
Author: User
Php string replacement, segmentation and connection methods, php string replacement and segmentation. Php string replacement, segmentation, and connection methods. php string replacement and segmentation this article describes how to replace, split, and connect php strings. For your reference, such as replacing, splitting, and connecting php strings, and replacing and splitting php strings.

This article describes how to replace, split, and connect php strings. We will share this with you for your reference. The details are as follows:

String replacement

1. search and replace a regular expression
The code is as follows: mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])

Search for the part in the subject that matches pattern and replace it with replacement.

2. substring replacement
The 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 after all the searches in the subject are replaced by replace.

String segmentation and connection

Use a regular expression to separate strings

Description

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

Use a regular expression to separate a given string.

2. explode-use one string to separate another string

Note:

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

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.