I have two types of strings, 'hello', 'helloThere'.
What I want is to change them so they read like: 'Hello', 'Hello There'
depending on the case.
What would be a good way fo doing this?
Thanks
I have two types of strings, 'hello', 'helloThere'.
What I want is to change them so they read like: 'Hello', 'Hello There'
depending on the case.
What would be a good way fo doing this?
Thanks
Use the ucwords
function:
Returns a string with the first character of each word in str capitalized, if that character is alphabetic.
The definition of a word is any string of characters that is immediately after a whitespace (These are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab).
This will not split up words that are slammed together - you will have to add spaces to the string as needed for this function to work.
PHP has many string manipulation functions. ucfirst()
would do it for you.
To convert CamelCase to different words:
preg_replace('/([^A-Z])([A-Z])/', "$1 $2", $string)
To uppercase all the words first letters:
ucwords()
So together:
ucwords(preg_replace('/([^A-Z])([A-Z])/', "$1 $2", $string))
use ucwords
<?php
$foo = 'hello world';
$foo = ucwords($foo); // Hello world
$bar = 'BONJOUR TOUT LE MONDE!';
$bar = ucwords($bar); // HELLO WORLD
$bar = ucwords(strtolower($bar)); // Hello World
?>