WEBVTT

00:00.470 --> 00:05.480
So next up, let's see how can we search and replace text in PHP.

00:05.510 --> 00:08.450
Let me define a haystack variable.

00:08.540 --> 00:14.060
That's a name that's often used for the string you're going to be searching in.

00:14.060 --> 00:20.480
And let's use a typical text which is the quick brown fox.

00:22.640 --> 00:23.180
Okay.

00:23.180 --> 00:29.360
Um, first let's see how can we get a position of a specific substring in a haystack?

00:30.800 --> 00:32.660
Let me define the post variable.

00:32.660 --> 00:37.340
And the Strpos function is going to give you this.

00:37.340 --> 00:42.920
So first you need to point to the haystack which is the string we're going to be searching in.

00:42.920 --> 00:46.970
And then the text that you want found.

00:48.890 --> 00:56.090
So now I dump this and we can see what's the position of this specific string fragment.

00:58.430 --> 01:01.010
Um let me try again.

01:01.010 --> 01:03.350
String search okay.

01:03.380 --> 01:10.450
So the position is four, Now, which is 01234.

01:10.450 --> 01:12.610
So it is zero indexed.

01:13.390 --> 01:13.600
Okay.

01:13.630 --> 01:16.330
Now let's see how can we replace the text.

01:16.330 --> 01:21.190
So let's maybe immediately vardump this without using any variables.

01:21.190 --> 01:24.430
So there is this str replace function.

01:25.150 --> 01:32.020
So I can replace maybe the text quick with a lazy.

01:32.470 --> 01:34.990
And I'm passing the haystack.

01:35.380 --> 01:38.470
And now let's run this.

01:39.820 --> 01:42.220
So this is how the text looks like.

01:42.220 --> 01:45.010
So this function is returning the new string.

01:45.010 --> 01:49.090
It's not doing anything to the original variable.

01:50.770 --> 01:54.520
Now also PHP supports the regular expressions.

01:54.520 --> 01:58.210
So regular expressions are not unique to PHP.

01:58.240 --> 02:00.280
That's a generic concept.

02:00.280 --> 02:06.190
And regular expressions let you match text using patterns.

02:06.190 --> 02:13.290
This means you are not searching for or replacing the literal text like in this case.

02:13.320 --> 02:19.380
Instead, you can provide a pattern and this pattern is evaluated.

02:19.380 --> 02:24.810
It's like a mini programming language inside a programming language.

02:25.140 --> 02:25.800
Maybe.

02:25.800 --> 02:27.480
Let's just see an example.

02:27.780 --> 02:35.220
So there are some specific functions like Prague match or Prague match or which we're going to use.

02:35.220 --> 02:44.460
This specific one will match all the instances of a specific pattern that can be found in a haystack,

02:44.760 --> 02:47.610
and you just need to pass it a variable.

02:47.610 --> 02:50.520
And this one is passed by reference.

02:50.610 --> 02:56.760
So we talked about references and how this function works is it will fill up.

02:56.760 --> 03:05.460
This matches variable with all the instances found based on this pattern that you provide here.

03:05.460 --> 03:08.820
So the regular expression needs two slashes.

03:08.820 --> 03:12.480
And then inside you can use some special symbols.

03:12.480 --> 03:22.510
So for example slash w means any character That is a word character that's an uppercase lowercase letter.

03:22.540 --> 03:25.000
Underscore and numbers.

03:25.090 --> 03:29.470
Next we've got quantifiers inside curly braces.

03:29.470 --> 03:34.360
I can say match all those characters that you have before.

03:34.360 --> 03:38.110
So this is a single word character five times.

03:38.110 --> 03:47.830
And since I'm running Pregmatch, all this will find all the instances of basically words that are,

03:48.550 --> 03:52.210
well, that length is five characters.

03:52.210 --> 03:55.210
So we are searching inside this text.

03:55.930 --> 04:02.410
This means I expect to find two words the quick and brown.

04:03.250 --> 04:04.420
So let me run this.

04:04.420 --> 04:09.580
Now the file is string search.

04:10.870 --> 04:14.260
Yeah, so I forgot to vardump this.

04:14.350 --> 04:21.220
So let's vardump matches and rerun that again.

04:22.030 --> 04:22.540
There it is.

04:22.540 --> 04:24.580
We've got quick and brown.
