I generated the following regex code with http://gskinner.com/RegExr/ where it works, but when I execute it with PHP, it fails to use the match in the replacement string. Output: Expected: I know that obviously the $& is not doing the correct thing, but how can I get it to work? Answer Try with this instead $0 means it will become