00:00
00:00
Newgrounds Background Image Theme

SpeakyDooman just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: Capitalize Every Word (arrays)

12,207 Views | 11 Replies
New Topic Respond to this Topic

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.

Response to Php: Capitalize Every Word (arrays) 2006-03-13 11:11:43


Sorry, I acidentally linked the wrong page, but it's a good read if you want to find out more on explode and implode.

Real PHP: Main

Response to Php: Capitalize Every Word (arrays) 2006-03-13 11:42:47


ucwords(), anyone?

Response to Php: Capitalize Every Word (arrays) 2006-03-13 11:46:03


At 3/13/06 11:42 AM, liljim wrote: ucwords(), anyone?

but then we wouldnt have to muddle through the monotony of a explode, ucfirst, implode combination. and we certainly wouldnt want to miss out on all that fun, now would we?


BBS Signature

Response to Php: Capitalize Every Word (arrays) 2006-03-13 11:50:06


At 3/13/06 11:42 AM, liljim wrote: ucwords(), anyone?

There he goes again, making stuff up.


Sup, bitches :)

BBS Signature

Response to Php: Capitalize Every Word (arrays) 2006-03-13 12:09:06


At 3/13/06 11:42 AM, liljim wrote: ucwords(), anyone?

Although I feel a bit ashamed now, I still think this tut wasn't a complete miss because I primarily wanted to explain to beginners how to use explode, foreach and implode to achieve the goal.

Response to Php: Capitalize Every Word (arrays) 2006-03-13 12:26:15


At 3/13/06 12:09 PM, juraj wrote:
Although I feel a bit ashamed now

You've nothing to feel ashamed about! :) I actually made the mistake of writing a similar function a while back, when I suddenly got an inkling that I'd seen something that handles that stuff, went to the entry for ucfirst() and sure enough, ucwords() was on the left menu, with the other string functions and in a list of other suggested functions in the main section.

I still think this tut wasn't a complete miss because I primarily wanted to explain to beginners how to use explode, foreach and implode to achieve the goal.

Right, right.

Response to Php: Capitalize Every Word (arrays) 2006-03-13 18:25:20


At 3/13/06 11:42 AM, liljim wrote: ucwords(), anyone?

Oh please James, everyone knows there's no such thing.


Merkd.com - It Pays to Play

Earn real money by betting and investing; or, sponsor, challenge, compete,

recruit, communicate, network, earn money playing games, and much more.

Response to Php: Capitalize Every Word (arrays) 2006-03-13 21:31:14


Reminds me of when I tried to make a function...when all I needed was strtolower()

Response to Php: Capitalize Every Word (arrays) 2006-03-13 22:43:17


its almost as clever as my date tutorial, lol

for those who don't know, i wrote a tutorial on dates and arrays a long time ago not even knowing the secrets behind the date() function ROFL

Response to Php: Capitalize Every Word (arrays) 2006-03-13 23:42:52


At 3/13/06 10:43 PM, Jams44 wrote:
for those who don't know, i wrote a tutorial on dates and arrays a long time ago not even knowing the secrets behind the date() function ROFL

Heh, I remember that :P

Response to Php: Capitalize Every Word (arrays) 2006-03-14 09:32:40


At 3/13/06 10:43 PM, Jams44 wrote: its almost as clever as my date tutorial, lol

You have a tutorial on dating? Yeah, I could use one.

for those who don't know, i wrote a tutorial on dates and arrays a long time ago not even knowing the secrets behind the date() function ROFL

Yes, when you want it complicated they want it simple. When you want it simple they want it complicated