note 25291 added to function.preg-replace

From: Date: Wed, 18 Sep 2002 10:20:57 +0000
Subject: note 25291 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-36885@lists.php.net to get a copy of this message
~~~ \n Characters in search strings: ~~~ It IS possible to use PCRE with \n characters inside the search string, just use the modifier /m for "multiline mode" (I too pulled some hair out to figure out that one, just read _carefully_ about PCRE Synthax) ~~~ Numbers following \\# ~~~ I experienced some problems with adding a date to a filename. The RegExp checked for any given directory before the filename, then looked up if there were 8 numbers (a date of YYYYMMDD), a - and a filename.php If the 8 numbers don't exist it adds them automatically: $Ymd_date = date('Ymd'); $regExp_3 = '/^(\/(.*\/)+?)([^\d]{8})(.*\.php)$/i'; $replacement_3 = "\\1".$Ymd_date.'-\\3\\4'; You would assume that it would replace \\1 to everything inside the first (), but no - adding a number (the date) it actually looks like this to the parser: "\\120020918-\\3\\4" The parser must think you're using \\12 ! I fixed that with a workaround: $Ymd_date = date('Ymd'); $regExp_3 = '/^(\/(.*\/)+?)([^\d]{8})(.*\.php)$/i'; $replacement_3 = "\\1_%_%_".$Ymd_date.'-\\3\\4'; Another str_replace('_%_%_','',$string); to the string preg_replace returned did it (unlikely to have _%_%_ in the filename). -- http://www.php.net/manual/en/function.preg-replace.php http://master.php.net/manage/user-notes.php?action=edit+25291 http://master.php.net/manage/user-notes.php?action=delete+25291 http://master.php.net/manage/user-notes.php?action=reject+25291

« previous php.notes (#36885) next »