Be a Supporter!

Php: Capitalize Every Word (arrays)

  • 7,777 Views
  • 11 Replies
New Topic Respond to this Topic
juraj
juraj
  • Member since: May. 15, 2004
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 11:06 AM Reply

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.

juraj
juraj
  • Member since: May. 15, 2004
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 11:11 AM Reply

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

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 11:42 AM Reply

ucwords(), anyone?

authorblues
authorblues
  • Member since: Jun. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 11:46 AM Reply

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
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 11:50 AM Reply

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

There he goes again, making stuff up.


Sup, bitches :)

BBS Signature
juraj
juraj
  • Member since: May. 15, 2004
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 12:09 PM Reply

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.

liljim
liljim
  • Member since: Dec. 16, 1999
  • Offline.
Forum Stats
Staff
Level 28
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 12:26 PM Reply

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.

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 06:25 PM Reply

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

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

WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Supporter
Level 15
Programmer
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 09:31 PM Reply

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

Jams44
Jams44
  • Member since: Nov. 8, 2004
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 10:43 PM Reply

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
WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Supporter
Level 15
Programmer
Response to Php: Capitalize Every Word (arrays) Mar. 13th, 2006 @ 11:42 PM Reply

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

juraj
juraj
  • Member since: May. 15, 2004
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
Response to Php: Capitalize Every Word (arrays) Mar. 14th, 2006 @ 09:32 AM Reply

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