WEBVTT

00:00.260 --> 00:00.740
Okay guys.

00:00.740 --> 00:06.560
So next up let's talk about string formatting and parsing.

00:06.950 --> 00:12.380
So let's start with the simple example the string formatting.

00:14.480 --> 00:20.720
So this would be something else than string concatenation or interpolation that we've seen before.

00:23.330 --> 00:25.790
Let me define a variable called Alice.

00:25.790 --> 00:27.170
So this would be a string.

00:27.170 --> 00:31.970
And another one called age which would be 30.

00:32.000 --> 00:39.770
Now there is a printf function which lets you define a string with some placeholders like percent s.

00:39.800 --> 00:42.830
This means you're going to pass a string in here.

00:42.830 --> 00:48.140
Then percent d means you're going to pass an integer.

00:48.140 --> 00:56.390
So this is something similar to what I think is also present in Cplusplus and Python.

00:56.390 --> 01:04.100
And then you just pass variadic arguments and they just need to match the position of those percent

01:04.100 --> 01:05.240
placeholders.

01:05.330 --> 01:07.190
So first is a string.

01:07.190 --> 01:08.840
So you pass a name here.

01:08.870 --> 01:11.740
Then there is an H.

01:13.690 --> 01:20.950
And if we run the string formatting this is what we get.

01:21.160 --> 01:25.090
So this is immediately printing that on the screen.

01:26.290 --> 01:29.170
Next up let's see an example.

01:29.170 --> 01:34.690
So data in CSV format comma separated values.

01:34.870 --> 01:41.170
So we can have apple banana and cherry.

01:41.350 --> 01:47.170
Now this CSV format is super common because that's actually the format in which you're going to get

01:47.170 --> 01:55.090
data when you export from let's say Excel and probably many other software programs.

01:55.090 --> 01:57.940
So that's something you often have to work with.

01:59.080 --> 02:05.350
So to convert this to something you can easily process with PHP, you'd like this in an array.

02:05.350 --> 02:11.350
So a common used function in PHP is explode which lets you use a separator like a comma.

02:11.350 --> 02:21.000
In this example, passing the string and this should give you an array you can Then work with.

02:21.030 --> 02:23.280
So that's fruits.

02:24.240 --> 02:26.460
Let me run that again.

02:26.880 --> 02:33.450
And the string was converted to such array apple banana and cherry.

02:35.070 --> 02:41.910
Now if you need to convert the array back to string or just convert an array to a string, you can also

02:41.910 --> 02:42.630
do that.

02:42.690 --> 02:50.670
So the opposite of explode is called implode where you can also pass a separator let's say comma and

02:50.670 --> 02:52.080
then the array.

02:52.170 --> 02:57.330
So fruits array should be now converted.

02:58.650 --> 02:59.460
And it is.

02:59.460 --> 03:01.740
So this is our new string.

03:04.440 --> 03:08.130
Next up let's see the string padding.

03:08.280 --> 03:15.720
Let me create a padded variable and call str underscore pad.

03:15.720 --> 03:21.180
So you probably have noticed the inconsistency in function naming in PHP.

03:21.210 --> 03:23.730
That's unfortunately the case.

03:23.730 --> 03:25.530
Can't really change that.

03:26.220 --> 03:32.600
And let's pad that with such character to length ten.

03:33.920 --> 03:42.410
And you can select how you want the padding to work either left side, right side, or both.

03:43.640 --> 03:44.960
So let's choose both.

03:44.960 --> 03:48.560
And maybe this time we're gonna echo that.

03:48.710 --> 03:54.500
So this is padded and now let's run it.

03:56.060 --> 04:00.470
So as you see we've got padding on the left and on the right.

04:00.470 --> 04:08.960
So this uh, second parameter is basically saying what should be the length of the resulting string.

04:09.530 --> 04:12.140
So we've got our string that we pass.

04:12.140 --> 04:18.620
Plus the remaining space is filled with this third character.

04:20.450 --> 04:27.440
So if I would go with 11 in this case we can have padding from both sides.

04:27.470 --> 04:29.360
That's equal size.

04:30.320 --> 04:38.090
So it's also pretty useful if you need some kind of fixed length fields or fixed length strings.

04:39.040 --> 04:43.300
And finally, let's see our trimming.

04:45.190 --> 04:47.140
I'm gonna have to dump that immediately.

04:48.460 --> 04:52.750
So let's call the trim function on a string looking like this.

04:52.750 --> 04:55.060
So we've got some spaces.

04:55.570 --> 04:57.130
Hello, world.

04:57.130 --> 04:59.920
And some spaces at the end.

04:59.980 --> 05:02.950
So trimming is something that you would.

05:02.980 --> 05:05.380
Often have to do with the user input.

05:05.380 --> 05:08.260
Because some users are just careless.

05:08.260 --> 05:09.010
They.

05:09.040 --> 05:11.770
Might enter some white spaces strings.

05:11.770 --> 05:15.610
And if you need your data to be normalized.

05:15.640 --> 05:22.840
You just need to make sure that someone or that you just remove the unnecessary.

05:22.870 --> 05:24.280
White spaces.

05:24.280 --> 05:27.820
Then trimming is what you need to do often.

05:27.820 --> 05:28.990
And also this.

05:28.990 --> 05:33.700
This is often used when you import data from CSV, for example.

05:33.730 --> 05:36.490
Okay, let's see the effect of that.

05:37.480 --> 05:38.380
So there it is.

05:38.380 --> 05:46.420
That's the string with all the white white spaces at the beginning and end of that string being stripped

05:46.420 --> 05:48.370
off A3.
