I am having a hard time to understand the preg_replace().
This is what I have got so far.
preg_replace($patter, $replacement, $string)
But, It confuses me when there is capturing groups in replacement.
But still I get some of it. Like below
preg_replace('/(\w+)/', 'hello', 'say hello!')
I know it will result in 'hello hello!'.
I can do something more with capturing group.
preg_replace('/(\w+)/','\1 Hello', 'Say World!',1)
It will result in 'Say Hello World'.
This is what I don't get.
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?>
Output:
April1,2003
What ${1}1,$3
means?
What's the difference between using $1
\1
and <\1>
What does this ${1}1
mean?