A simple preg_match replacement on the Internet $ string & nbsp ;=& nbsp; "Is & nbsp; is & nbsp; the & nbsp; cost & nbsp; of & nbsp; of & nbsp; gasoline & nbsp; going & nbsp; up "; & nbsp; $ the replacement of a simple preg_match on pa is unknown.
$string = "Is is the cost of of gasoline going up up";
$pattern = "/\b([a-z]+) \\1\b/i";
if(preg_match($pattern, $string,$arr)){
print_r($arr);
echo preg_replace($pattern, '$1', $string);
}
Output
Array
(
[0] => Is is
[1] => Is
)
Is the cost of gasoline going up
In my understanding, preg_match ($ pattern, $ repalce, $ subject) uses pattern to match each group from subject, and then uses the display method specified by replace to re-output
$ Pattern = "/\ B ([a-z] +) \ 1 \ B/I" only matches Is and is
How can I output the entire sentence "Is the cost of gasoline going up" again?
When can I use $1 and \ 1 to obtain the sub-pattern matching content? what are the requirements in double quotation marks and single quotation marks? Php? Regular expression sharing:
------ Solution --------------------
Preg_match match once,
While
Mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])
When the third parameter is not specified, the default value is-1 (unlimited ).
Difference between $1 and \ 1:
\ 1 can be used in both the mode and replacement string
$1 can only be used to replace strings
------ Solution --------------------
Preg_match only matches the first match. to use preg_match_all for all matches
Replace: replace all matching items (the matching part is the same as preg_match_all)
$ And \ are just habits. Generally, the expressions in the same regular expression are close to perl.
Pay attention to the original \ for a single double quotation mark. for other non-ambiguity, press the original
------ Solution --------------------
$ Pattern ="
------ Solution --------------------
<[^>] +> (.*) ] +>
------ Solution --------------------
";
$ Pattern = "/<[^>] +> (. *) <\/[^>] +> /";
Is the same
If the delimiter is/, escape the/in the rule string.
Fortunately, php provides custom delimiters to make the rule string look comfortable.
If JavaScript is used, it would be less fortunate.