Kshitij Verma
Kshitij Verma

Reputation: 23

How can I put characters around a word which begins with a specific character?

For example I have a string : "This is a !thing"

How can I make it like this - "This is a !<b>thing</b>"

Any help would be appreciated.

Thanks guys.

Upvotes: 2

Views: 61

Answers (1)

Barmar
Barmar

Reputation: 781096

preg_replace('/!(\w+)/', '!<b>$1</b>', $string);

In the replacement string $n is replaced with whatever matched nth capture group in the regexp.

Upvotes: 3

Related Questions