1
2<?php
3$foo = 'hello world!';
4$foo = ucfirst($foo); // Hello world!
5
6$bar = 'HELLO WORLD!';
7$bar = ucfirst($bar); // HELLO WORLD!
8$bar = ucfirst(strtolower($bar)); // Hello world!
9?>
10
11
1$foo = 'hello world!';
2$foo = ucwords($foo); // Hello World!
3
4$bar = 'HELLO WORLD!';
5$bar = ucwords($bar); // HELLO WORLD!
6$bar = ucwords(strtolower($bar)); // Hello World!
7
8//With custom delimiter
9$foo = 'hello|world!';
10$bar = ucwords($foo); // Hello|world!
11
12$baz = ucwords($foo, "|");
1/*
2 This only Capitalizes words in a string that are entirely alphabetic
3 and other words are made UPPERCASE
4 Works well if you have a string of words containing a mixture
5 of English words and part codes etc
6*/
7$words = explode(" ", $originalString);
8$finalString = "";
9 foreach($words as $word) {
10 if(ctype_alpha($word)) {
11 $word = ucfirst(strtolower($word));
12 }
13 else {
14 $word = strtoupper($word);
15 }
16 $finalString .= $word." ";
17 }
18echo rtrim($finalString);