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.
use a
preg_replace()with back references: http://php.net/manual/en/function.preg-replace.php