note 127182 deleted from function.preg-replace by crell
| From: | crell@php.net | Date: | Fri, 01 Jul 2022 19:43:49 +0000 |
| Subject: | note 127182 deleted from function.preg-replace by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218849@lists.php.net to get a copy of this message | ||
Note Submitter: jcours
----
Be careful about anchoring the start of your search expressions: an unanchored "match
everything" can produce unexpected results.
PHP version 7.3.33
preg_replace('/.*/', 'foo$0', '_X_') -> "foo_X_foo"
preg_replace('/^.*/', 'foo$0', '_X_') -> "foo_X_"
preg_replace('/(.*)/', 'foo$1', '_X_') -> "foo_X_foo"
preg_replace('/^(.*)/', 'foo$1', '_X_') -> "foo_X_"
<?php
echo "PHP version " . PHP_VERSION . "\n";
foreach(array('/.*/' => 'foo$0', '/^.*/' => 'foo$0',
'/(.*)/' => 'foo$1', '/^(.*)/' => 'foo$1') as $pat
=> $rep) {
$subj = '_X_';
$rslt = preg_replace($pat, $rep, $subj);
printf("preg_replace('%s', '%s', '%s') ->
\"%s\"\n",
$pat, $rep, $subj, $rslt);
}
?>