So we have almost reached the end of this project and this video is optional.
But before you decide if you want to watch it or not, let me explain what it is about.
So we are at the guestbook page and you can see I am displaying the names and emails in the open.
That's not good.
We don't want the emails that people have sent to us to be picked up by any robots, or any automated
bots that can use those emails to spam those people.
And in this video, we will obfuscate this data inside the view.
So that's why I say this is optional, but it might be an interesting exercise on how do you work with
strings.
So either stay or you can skip this one.
Now I think you are staying.
So let's jump to the guestbook router.
So we're not gonna be doing anything in here.
I think we can actually add some view logic.
So in here with the messages I'm going to add some functions that will obfuscate the email and the name.
And we're just going to use those functions every single time we render something.
By the way let's have the docs open.
And let's begin by obfuscating the name.
Let me add a function called obfuscate name.
I'm starting with this one because it's just would be easier to return a string from it.
So we need to use the str Len which is checking the length of a string.
If this is less or equal to.
Then you shouldn't really obfuscate anything as there is no point.
I think there are some names that are this short.
It might be a nickname, or I think some Asian names are this short now.
In the other case, first we're gonna just get the first letter of the name.
For that, we're gonna use another function called substring, which basically lets us get the part
of a string.
So from name we are starting at an offset which is zero based.
So I get the position zero which is the first letter and the length is one.
Then I'm gonna generate some characters using str repeat.
This allows me to construct a string from a character or from a string in general.
So first a string to repeat that would be an asterisk.
How many times we'd like to repeat this one?
Well, maybe let's use str Len again.
And I'm going to get the length of the name minus two.
That's because I'd like to display the first and the last name.
This means we need to finish off with another substring call for the name, and I can use negative offset
which would basically move to the end of the string if I want the last letter.
And I don't need to pass the length here because minus one is essentially moving from the end to, let's
say this point.
So from this string string I'm moving before G and then I'm going till the end.
So that way I'm just getting the last letter.
Let's add a semicolon in here and let's see if this works.
Where is the name?
The name is here we use HTML special chars as well.
And I'm going to obfuscate the name.
Now let's jump back to our app.
And this is how it looks like.
Okay so this was fine.
Next up let's work on obfuscating the email.
So we do obfuscate email.
We need to get the string argument and we return the string as well.
So here well we need to be wary of the domain.
So let's get the parts of this email.
First we're going to be using explode because with emails we are sure that there is an ad character,
it has to be there.
So we separate the obfuscate that part and this part.
So from the email we are getting the parts.
Now the username is the first part which would be indexed with zero and domain is the second part.
So just to be sure we ask for the part with index one or if there is none.
Somehow someone sneaked in an incorrect email.
We get the default, which is an empty string.
Okay, now let's obfuscate the username because I think we can keep the domain.
That's not secret.
So I can overwrite username.
I think this is fine.
So maybe let's get first two characters from the username.
So starting at the index zero and taking two characters, we're going to join that with an STR repeat
call where we repeat the asterisk.
And then again we're going to get the str Len.
So the length of the username minus two.
We do that because here we just got the first two characters from the username.
Now it might happen that the username is shorter than this.
And for that cases not to have a negative number we're going to use the max function to pick the value
that is bigger.
That would be either zero or the length of the string minus two.
So in case this ends up being negative, we just say that we repeat this asterisk zero Times.
Okay.
Let's add a semicolon.
And finally we just need to return the username at domain.
And we are done.
Now let's see if this works fine.
So where is the email.
It's here.
So I'm calling the obfuscate email and passing the data.
And this is how it looks like I think it's fine.
We just get two characters from the email and the rest are the asterisks.
We don't need to match the amount of characters ideally, because it doesn't really matter.
The most important part is that we hide some sensitive information to which we have access to, but
our visitors don't need that.
Okay, we are done.