Reputation: 3341
I need to transform some string from:
big red car
to:
(big) (red) (car)
I know that I can do this with explode()
and implode()
functions or foreach
cycle but I'm interested in a regular expression
solution.
Upvotes: 0
Views: 140
Reputation: 19423
You can use preg_replace()
for this.
First match those words with:
\b(\w+)\b
Then replace with:
($1)
In PHP:
preg_replace('/\b(\w+)\b/', "($1)", $input);
Upvotes: 3