BEGINNER - INTERMEDIATE
PHP: Capitalize Every Word (utilizing arrays, foreach, implode and ucfirst)
Visit PHP: Main for more tutorials
Today kids we'll learn how to transform normal, human sentences in retarded alien "Every Word Is Capitalized" sentences often used in marketing. It's also a good example of combining explode, foreach and implode!
This is our final product:
function capitalize_words($string) {
$string = explode(' ', $string);
foreach($string as $key => $value) {
$string[$key] = ucfirst($string[$key]); }
return implode(' ', $string); }
Let's get started. Here's the basic code:
<?php
$string = 'Capitalize this, you asshole!';
$string = explode(' ', $string);
foreach($string as $key => $value) {
$string[$key] = ucfirst($string[$key]); }
echo implode(' ', $string);
?>
Let's examine this.
$string = 'Capitalize this, you asshole!';
Here we set up a variable which we want to capitalize.
$string = explode(' ', $string);
This is a fairly important part: we 'slice' the string using explode(). What does this actually do? (skip this if you know about explode())
We have the string 'Capitalize this, you asshole!'. Php will break it wherever it finds a space(we told PHP it's a space, remember? It's the first argument of the 'explode' function) into a numerical array. What this means is that every word is now separated and is in it's 'cell', which are starting from 0. We call it a 0-index array because of that. So, if we want to see the first word, we can say $string[0]! If we want to access the second word then it's $string[1]! What is $string[2]? It's 'you', because if we slice the sentence on every space, 'you' will be the third chunk.
So now we have an entire sentence in an array. Let's see what goes next!
foreach($string as $key => $value) {
$string[$key] = ucfirst($string[$key]); }
What is nifty about arrays is that you can do anything with it, and work with each 'cell' individually! You guessed it, we capitalize each cell. We use the FOREACH statement for this: foreach($string as $key => $value)
So, $string is the array to be looped through. $key is the array key. Remember when we accessed the word you with the key 2? On each iteration PHP will store the current key in that variable, so we can do stuff with it! And the last and literally least, the variable $value will have the value of the current key. That's the same thing if we say this: $string[$key]. We won't be using $value here because we need to modify the array directly, and changing $value doesn't affect the array itself. It is possible, though, to make $value a reference to the array, but we won't go now into that.
$string[$key] = ucfirst($string[$key]); }
So we opened the block of code which gets executed on every iteration with {. Here we access the current word using $string[$key] and capitalize the first letter using the function ucfirst! And that's it.
Note that we could have done something like this, too!
$string[$key][0] = strtoupper($string[$key][0]); // this is not found in original code!
What is this now? You see, every string is an array itself. It consists of letters, and every letter can be accessed with a key! If we decided to use the strtoupper function, which makes the entire WORD uppercase (unlike ucfirst, which does only the first letter), we need to do it only on the first letter! And we can access every string as an array this way, and we can access even arrays within arrays! So $string[0][0] gives us the first letter of the first word, and $string[1][0] gives us the first letter of the second word and so on! Are you imagining a script which returns the initials of a given name? Try to do that for your homework. ;)
Anyway we now have a processed array. We're now finished with it! But we can't return an array to the user if he has given us a string. So we need to give him a string too! We use the implode function which acts like a glue. We have sliced the array with spaces with explode, we need to glue it with spaces too with implode!
echo implode(' ', $string);
The last part is to sexify the code to a function. If you want to use that piece of code a whole lot of times in your code, transform it to a function and access it with a name, like you use explode, implode, print and such!
function capitalize_words($string) {
$string = explode(' ', $string);
foreach($string as $key => $value) {
$string[$key] = ucfirst($string[$key]); }
return implode(' ', $string); }
As you see, I have replaced echo with return. That means our function won't print the processed sentence, but it will return it to you so you can do stuff with it. It's like functions in math!
And a special extra: the initials function. Figure it out yourself, I've just done homework for you! ;)
function get_initials($name) {
$name = explode(' ', $name);
foreach($name as $key => $value) {
$initials .= $name[$key][0] . '.'; }
return $initials; }
And a special note: $me .= 1 is shortened $me = $me . 1. The assignment operator .= appends the right side to the left.