PHP string substitution, segmentation and connection method _php tips

Source: Internet
Author: User
Tags explode mixed

This article illustrates the substitution, segmentation, and connection methods of PHP strings. Share to everyone for your reference, specific as follows:

Substitution of strings

1. Perform search and replace of a regular expression

Copy Code code as follows:
Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])

Searches the part of the subject that matches the pattern and replaces it with replacement.

2. Substring substitution

Copy Code code as follows:
Mixed Str_replace (mixed $search, mixed $replace, mixed $subject [, int & $count])

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

Segmentation and concatenation 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-use a string to split another string

Description

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

$str = ' One|two|three|four ';
Positive limit
print_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] = > Two
  [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

For more information about PHP interested readers can view the site topics: "PHP array Operation skills Encyclopedia", "PHP Data structure and algorithm tutorial", "PHP Mathematical Operation Skills Summary", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.