Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
String Manipulation.docx59.1 KB String Manipulation - Solution.docx
59.2 KB
Quick reference
String Manipulation
There are several useful built in PHP functions that allow us to manipulate strings.
When to use
Any time you want to manipulate strings, try one of these functions.
Instructions
Here are some functions to change upper/lower case of strings:
strtoupper($var); // changes all letters to uppercase
ucwords($var); // changes all first letters to uppercase
ucfirst($var); // changes just first letter of first word to uppercase
strtolower($var); // changes all to lowercase
lcfirst($var); // changes first letter of first word to lowercase
This function allows you to replace anything in a string:
str_replace(old, new, variable);
Just change old to the thing you want to change, change new to the new thing, and variable is the source of the string you are changing.
Hints & tips
- str_replace(old, new, variable) to change anything in a string.
- strtoupper($var);
- ucwords($var);
- ucfirst($var);
- strtolower($var);
- lcfirst($var);
- 00:04 In this video I wanna talk about string manipulation.
- 00:07 So let's create a variable, I'm just gonna call it var.
- 00:10 And let's create a string, and let's call it John Elder.
- 00:15 We can programmatically do all kinds of cool things to this string,
- 00:19 we can manipulate it in all kinds of different ways.
- 00:21 And that's what we're going to talk about in this video.
- 00:23 Now like I said in a couple videos ago,
- 00:25 PHP has a lot of built-in functions, we're gonna look at a few of them.
- 00:28 The first one we're gonna look at is something called strtoupper.
- 00:32 And that's S-T-R, that stands for string, and then to, and then upper.
- 00:38 Now we can pass in our variable as the parameter of our function.
- 00:42 And if we echo this out and save this, so here's our old.
- 00:49 And you can see now now its changed all of them to uppercase.
- 00:53 And in fact, let's go ahead and echo out our variable.
- 00:57 Plus a line break, and
- 01:01 let's go old, there we go.
- 01:07 So now we get to see what it was.
- 01:09 Okay, so we have the old and
- 01:10 we can see the strtoupper changes everything to uppercase, so that's useful.
- 01:14 The next one we're gonna look at is ucwords.
- 01:18 And actually I'm gonna change our original string to lowercase.
- 01:23 So ucwords, uc stands for uppercase, and that should be words.
- 01:28 So we save this, hit Reload, we see it's changed,
- 01:32 this is the old lowercase lowercase.
- 01:35 The first letter of each word is changed to uppercase, so that's ucwords.
- 01:39 Next is ucfirst,
- 01:41 and what that does is uppercase the very first letter of only the first word.
- 01:46 If we reload, we see it changed the J in John to uppercase but not the e in elder.
- 01:52 Next we have strtolower, and
- 01:55 I'm gonna go ahead and change this to uppercase again.
- 02:00 Save this, now hit Reload, so
- 02:03 it's changing the uppercase to lowercase, that's useful, strtolower.
- 02:07 And finally we have lcfirst.
- 02:13 And that's just gonna change the very first letter of the very first word of
- 02:16 the lowercase and leave the rest.
- 02:18 So, strtoupper, ucwords, ucfirst, strtolower, lcfirst,
- 02:23 all very useful built-in functions to manipulate strings in different ways.
- 02:27 And there's lots of reasons why you might wanna use all of those things.
- 02:30 I'll put all these in the resource file so
- 02:31 you can copy them down a little more easily, that's string manipulation.
- 02:35 One more thing I wanna talk about very quickly is str_replace.
- 02:38 I'm gonna change this to John Elder is a PHP God.
- 02:44 We can change anything we want in this string by using str_replace.
- 02:48 And that's str_replace.
- 02:51 And here's our function, we pass in three things.
- 02:55 First the old, so I wanna change God, comma.
- 02:58 Now what do we wanna change it to?
- 03:01 Let's change it to Dork, and what are we changing?
- 03:04 Well we're changing our variable.
- 03:06 So if we echo this out and save it, hit Reload.
- 03:12 So here's our old, John Elder is a PHP God.
- 03:15 It's changed it to John Elder is a PHP Dork.
- 03:18 So a str_replace is very, very powerful.
- 03:20 There's lots of times when you wanna change some little thing in a string, and
- 03:24 that's what you use to do it, str_replace.
- 03:26 Just remember, old, new, and then the original source variable.
- 03:31 You could also do, I think, John Elder is a PHP God.
- 03:38 You don't need to use a variable, you can put the string right in there itself,
- 03:41 I believe.
- 03:42 Yep, hit Reload and it changes it.
- 03:45 So use variables, use the strings itself, it doesn't really matter.
- 03:48 Those are a few ways to manipulate strings, very useful.
- 03:52 In the next video we're gonna look at the include function.
Lesson notes are only available for subscribers.