PHP preg_replace function problems
$string = "april 15, 2003";$pattern = "/(/w+) (/d+), (/d+)/i";$replacement = "/${1}1,/$3";print preg_replace($pattern, $replacement, $string);/* output ======april1,2003*/
$ Replacement = "/$ {1} 1,/$3 ";
"/$ {1} 1,/$3" what does this mean?
$patterns = array ("/(19|20)(/d{2})-(/d{1,2})-(/d{1,2})/", "/^/s*{(/w+)}/s*=/");$replace = array ("//3///4///1//2", "$//1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");
$ Replace = array ("// 3 // 4 /// 1 // 2", "$ // 1 ="); what does this mean?
Reply to discussion (solution)
$ {1} is the first matching, and $3 is the third matching.
$ {1} is the first matching, and $3 is the third matching.
The second code is wrong. how can I output $ startdate = 5/27/1999?
$ {1} is the first matching, and $3 is the third matching.
$ {1} is there an escape \ slash in front of it not the original output?
The first piece of code is wrong and should be written
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = "\${1}1,\$3";print preg_replace($pattern, $replacement, $string);Or
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = '${1}1,$3';print preg_replace($pattern, $replacement, $string);
Similarly, the second paragraph of code should be written.
$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/");$replace = array ("\\3/\\4/\\1/\\2", "$\\1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");
$ N or \ n are backward referenced marks, indicating the content in the nth parentheses
The first piece of code is wrong and should be written
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = "\${1}1,\$3";print preg_replace($pattern, $replacement, $string);Or
$string = "april 15, 2003";$pattern = "/(\w+) (\d+), (\d+)/i";$replacement = '${1}1,$3';print preg_replace($pattern, $replacement, $string);
Similarly, the second paragraph of code should be written.
$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/");$replace = array ("\\3/\\4/\\1/\\2", "$\\1 =");print preg_replace ($patterns, $replace, "{startdate} = 1999-5-27");
$ N or \ n are backward referenced marks, indicating the content in the nth parentheses
Thank you. $ {1} does the previous escape slash \ mean the original output?
In double quotes, php tries to interpret $ as the leading variable.
The string "$ {1}" will cause an undefined variable error.
So escape the code so that $ {1} can be handed over to prea_replace for processing.
In double quotes, php tries to interpret $ as the leading variable.
The string "$ {1}" will cause an undefined variable error.
So escape the code so that $ {1} can be handed over to prea_replace for processing.
Thank you.