So next up, let's see how can we search and replace text in PHP.
Let me define a haystack variable.
That's a name that's often used for the string you're going to be searching in.
And let's use a typical text which is the quick brown fox.
Okay.
Um, first let's see how can we get a position of a specific substring in a haystack?
Let me define the post variable.
And the Strpos function is going to give you this.
So first you need to point to the haystack which is the string we're going to be searching in.
And then the text that you want found.
So now I dump this and we can see what's the position of this specific string fragment.
Um let me try again.
String search okay.
So the position is four, Now, which is 01234.
So it is zero indexed.
Okay.
Now let's see how can we replace the text.
So let's maybe immediately vardump this without using any variables.
So there is this str replace function.
So I can replace maybe the text quick with a lazy.
And I'm passing the haystack.
And now let's run this.
So this is how the text looks like.
So this function is returning the new string.
It's not doing anything to the original variable.
Now also PHP supports the regular expressions.
So regular expressions are not unique to PHP.
That's a generic concept.
And regular expressions let you match text using patterns.
This means you are not searching for or replacing the literal text like in this case.
Instead, you can provide a pattern and this pattern is evaluated.
It's like a mini programming language inside a programming language.
Maybe.
Let's just see an example.
So there are some specific functions like Prague match or Prague match or which we're going to use.
This specific one will match all the instances of a specific pattern that can be found in a haystack,
and you just need to pass it a variable.
And this one is passed by reference.
So we talked about references and how this function works is it will fill up.
This matches variable with all the instances found based on this pattern that you provide here.
So the regular expression needs two slashes.
And then inside you can use some special symbols.
So for example slash w means any character That is a word character that's an uppercase lowercase letter.
Underscore and numbers.
Next we've got quantifiers inside curly braces.
I can say match all those characters that you have before.
So this is a single word character five times.
And since I'm running Pregmatch, all this will find all the instances of basically words that are,
well, that length is five characters.
So we are searching inside this text.
This means I expect to find two words the quick and brown.
So let me run this.
Now the file is string search.
Yeah, so I forgot to vardump this.
So let's vardump matches and rerun that again.
There it is.
We've got quick and brown.