append and prepend text to a string
php, regex, string
Solution
use a `preg_replace()` with back references: http://php.net/manual/en/function.preg-replace.php
preg_replace("/cats/smi","($0)",$string);
Problem
I want to be able to search for a certain word in a string, and append and prepend characters to every instance of that word. example: ``` I like cats, cats are awesome! I wish I had cats! ``` becomes: ``` I like (cats), (cats) are awesome! I wish I had (cats)! ``` I know I could use ``` str_replace( 'cats', '(cats)', $string ); ``` but I would have to write "cats" twice. I want a method that only requires me to write it once.