note 127182 added to function.preg-replace

From: Date: Fri, 17 Jun 2022 20:13:18 +0000
Subject: note 127182 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-218597@lists.php.net to get a copy of this message
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); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.12.131 (proxied: 64.98.192.17) ---- Manual Page -- https://php.net/manual/en/function.preg-replace.php Edit -- https://main.php.net/note/edit/127182 Del: integrated -- https://main.php.net/note/delete/127182/integrated Del: useless -- https://main.php.net/note/delete/127182/useless Del: bad code -- https://main.php.net/note/delete/127182/bad+code Del: spam -- https://main.php.net/note/delete/127182/spam Del: non-english -- https://main.php.net/note/delete/127182/non-english Del: in docs -- https://main.php.net/note/delete/127182/in+docs Del: other reasons-- https://main.php.net/note/delete/127182 Reject -- https://main.php.net/note/reject/127182 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#218597) next »