…and to elaborate on the commas and colons
Initially you had
1.
$text = "Author: Steinbeck, John";
$pattern = "/(\w+), (\w+)/";
Which you changed to
2.
# $text is unchanged
$text = "Author: Steinbeck, John";
$pattern = "/(\w+): (\w+)/";
And then the output from 1. and 2. above are
Author: John Steinbeck
Steinbeck Author, John
If you have a closer look at the end result, you will find that it's not only the : that disappears, but that you actually have a completely different ordering of words…
Now, to make it easier to see what happens, use preg_match instead to see what the pattern and its capturing subpatterns actually match
if (preg_match($pattern, $text, $m))
printf('<pre>%s</pre>', print_r($m,1));
Which for cases 1. and 2. above will yield
Array
(
[0] => Steinbeck, John
[1] => Steinbeck
[2] => John
)
Array
(
[0] => Author: Steinbeck
[1] => Author
[2] => Steinbeck
)
And what preg_replace does is:
Inside the string $text (Author: Steinbeck, John)
Replace everything matched (the [0] element above)
With "$2 $1" (the second matched subpattern followed by the first matched subpattern - elements [2] and [1] above)
So the two cases are
1.
Inside the string "Author: Steinbeck, John"
Replace "Steinbeck, John"
with "John Steinbeck"
2.
Inside the string "Author: Steinbeck, John"
Replace "Author: Steinbeck"
with "Steinbeck Author"