The specified character of the variable in php regular expression replacement. The following three implementations are used to introduce the specified characters of variables in php regular expression replacement. For more information, see. Three methods. 1. the code is as follows: Copy the code? Under php $ strpreg_qu, three implementations are provided to introduce the specified characters of variables in php regular expression replacement. For more information, see.
Three methods.
1.
| The code is as follows: |
|
|
$ Str = preg_quote ('(silver )');
$ Txt = 'My name (silver )';
Echo preg_replace ("/($ str)/", "$1", $ txt );
?> |
2.
| The code is as follows: |
|
|
$ Str = quotemeta ('(silver )');
$ Txt = 'My name (silver )';
Echo preg_replace ("/($ str)/", "$1", $ txt );
?> |
3.
| The code is as follows: |
|
$ Str = '(silver )';
$ Txt = 'My name (silver )';
Echo preg_replace ("/(Q $ strE)/", "$1", $ txt );
?>
|
All three methods return the same result. The Perl-style regular expression in PHP is exactly the same as that in Perl. quotemeta is also common ..
Some other regular expressions
Example:
| The code is as follows: |
|
|
$ Text = "foobar123fooabcbar ";
$ Text = preg_replace ("/foo (? = Bar)/"," *** ", $ text ); |
// Match the position in front of bar *** bar123fooabcbar
| The code is as follows: |
|
|
$ Text = "foobar123fooabcbar ";
$ Text = preg_replace ("/(? <= Bar) 123/"," *** ", $ text ); |
// Match the location foo behind the bar *** 123 fooabcbar
| The code is as follows: |
|
|
$ Text = "foobar123fooabcbar ";
$ Text = preg_replace ("/foo (?! Bar)/"," *** ", $ text ); |
// Match the position behind the bar instead of foobar123 *** abcbar
| The code is as follows: |
|
|
$ Text = "foobar123fooabcbar ";
$ Text = preg_replace ("/(? |
// Match foobar123fooabc, which is not the position of foo ***
Bytes. Three methods. 1. the code is as follows? Php $ str = preg_qu...