Okay guys.
So next up let's talk about string formatting and parsing.
So let's start with the simple example the string formatting.
So this would be something else than string concatenation or interpolation that we've seen before.
Let me define a variable called Alice.
So this would be a string.
And another one called age which would be 30.
Now there is a printf function which lets you define a string with some placeholders like percent s.
This means you're going to pass a string in here.
Then percent d means you're going to pass an integer.
So this is something similar to what I think is also present in Cplusplus and Python.
And then you just pass variadic arguments and they just need to match the position of those percent
placeholders.
So first is a string.
So you pass a name here.
Then there is an H.
And if we run the string formatting this is what we get.
So this is immediately printing that on the screen.
Next up let's see an example.
So data in CSV format comma separated values.
So we can have apple banana and cherry.
Now this CSV format is super common because that's actually the format in which you're going to get
data when you export from let's say Excel and probably many other software programs.
So that's something you often have to work with.
So to convert this to something you can easily process with PHP, you'd like this in an array.
So a common used function in PHP is explode which lets you use a separator like a comma.
In this example, passing the string and this should give you an array you can Then work with.
So that's fruits.
Let me run that again.
And the string was converted to such array apple banana and cherry.
Now if you need to convert the array back to string or just convert an array to a string, you can also
do that.
So the opposite of explode is called implode where you can also pass a separator let's say comma and
then the array.
So fruits array should be now converted.
And it is.
So this is our new string.
Next up let's see the string padding.
Let me create a padded variable and call str underscore pad.
So you probably have noticed the inconsistency in function naming in PHP.
That's unfortunately the case.
Can't really change that.
And let's pad that with such character to length ten.
And you can select how you want the padding to work either left side, right side, or both.
So let's choose both.
And maybe this time we're gonna echo that.
So this is padded and now let's run it.
So as you see we've got padding on the left and on the right.
So this uh, second parameter is basically saying what should be the length of the resulting string.
So we've got our string that we pass.
Plus the remaining space is filled with this third character.
So if I would go with 11 in this case we can have padding from both sides.
That's equal size.
So it's also pretty useful if you need some kind of fixed length fields or fixed length strings.
And finally, let's see our trimming.
I'm gonna have to dump that immediately.
So let's call the trim function on a string looking like this.
So we've got some spaces.
Hello, world.
And some spaces at the end.
So trimming is something that you would.
Often have to do with the user input.
Because some users are just careless.
They.
Might enter some white spaces strings.
And if you need your data to be normalized.
You just need to make sure that someone or that you just remove the unnecessary.
White spaces.
Then trimming is what you need to do often.
And also this.
This is often used when you import data from CSV, for example.
Okay, let's see the effect of that.
So there it is.
That's the string with all the white white spaces at the beginning and end of that string being stripped
off A3.