Andra
Andra

Reputation: 1392

Regular Expression for comma separated words

I want to replace some string like

Category : Entertaiments, Movie,Music, Super Natural, Hobbies

(even with inconsistent comma and space placing) To be like this

Category : <a href="http://someurl/entertaiments">Entertaiments</a>, <a href="http://someurl/movie">Movie</a>, <a href="http://someurl/music">Music</a>, <a href="http://someurl/super-natural">Super Natural</a>, <a href="http://someurl/hobbies">Hobbies</a>

I already tried to use \w+(?=[,]), but it cant work perfectly

Upvotes: 1

Views: 278

Answers (1)

Dmitry
Dmitry

Reputation: 2087

If you need use regex try this:

$string = 'Category : Entertaiments, Movie,Music, Super Natural, Hobbies';
$string = preg_replace('/,(\S)/',', $1', $string);

Upvotes: 2

Related Questions