Vimzy
Vimzy

Reputation: 1965

How to Use preg_replace to make spaces and capitals?

I'm trying to convert a variable that say has, "hello-big-boy" or "hello-big" or even just say, "hello" into a variable that would have all the dashes converted to spaces, and the first letter of each word as a capital. So "hello-big-boy" would become Hello Big Boy, and "hello-big" would become Hello Big, and "hello" would become Hello.

Any help is much appreciated guys! Thanks :)

Upvotes: 1

Views: 46

Answers (1)

Since there is only a minor replace , you could avoid making use of regex and go with the native PHP functions.

<?php
$str='hello-big-boy';
echo ucwords(str_replace('-',' ',$str)); //"prints" Hello Big Boy

Demo

Upvotes: 1

Related Questions