I don't know what you want to match with .*
. You don't need it. This will work fine:
preg_replace('/(lets go)/', "going", "lets go somewhere");
Or you can use a lazy match:
preg_replace('/(lets go).*?/', "going", "lets go somewhere");
Explanation: Your original expression was greedy. That roughly means that .*
matches as many characters as possible. .*?
is lazy; it matches the minimum amount of characters.
You can also match 'somewhere' as a sub-pattern, and us it in the replacement:
preg_replace('/(lets go)(.*)/', "going\$2", "lets go somewhere");
Here. $0 is "lets go somewhere", $1 is "lets go", $2 is " somewhere". The backslash is needed because "going\$2" is inside double quotes.