Koralek M.
Koralek M.

Reputation: 3341

PHP regexp - put every word into the brackets

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

Answers (1)

Ibrahim Najjar
Ibrahim Najjar

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

Related Questions