Php regular ereg_replace replacement

Source: Internet
Author: User
Tags php tutorial regular expression

Ereg_replace -- Regular expression replacement (php Tutorial 3, php 4, php 5)

String ereg_replace (string pattern, string replacement, string)

This function scans the part matching pattern in string and replaces it with replacement.

Returns the replaced string. (If there is no matching item to be replaced, the original string is returned .)


<? Php
$ String = "this is a test ";
Echo str_replace ("is", "was", $ string );
Echo ereg_replace ("() is", "1was", $ string );
Echo ereg_replace ("() is)", "2was", $ string );
?>

The output is as follows:
That was a test
That was a test
That was a test


<Td class = 'title'> popular oou limited edition bath towel for two </td>
Replace the whole with null or
<Td class = 'title'> popular oou limited edition bath towel for two </td>
Replace
<Td class = 'title'> </td>

First

Echo preg_replace ('/(<td [^ <>] + title [^ <>] +>) [^ <>] * (</td>)/I ', '', $ html );


Second:

Echo preg_replace ('/(<td [^ <>] + title [^ <>] +>) [^ <>] * (</td>)/I ', '$1 $2', $ html );


First, this regular expression matches similar formats:
<Td * title *> * </td>. Here, each asterisk * represents multiple arbitrary characters, which is equivalent to [^ <>] + in each * corresponding to the regular expression, for exact match, any character here does not contain '<', '> '.
For the replacement strings $1 and $2 in the second type, they are the matching values in the two groups () corresponding to the regular expression respectively. in this form of regular expressions, it is called subpattern matching. $1 and $2 are called reverse matching results.

Here, the result of $1 matching is <td class = 'title'>, and the result of $2 matching is </td>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.