Throughout this series, I have been using the ‘/’ character as a start and end of my pattern, however there are other perfectly valid ones too…
- /
- @
- #
- `
- ~
- %
- &
- ‘
- “
Throughout this series, I have been using the ‘/’ character as a start and end of my pattern, however there are other perfectly valid ones too…
Thats right, we may want to know if something is behind something else… And a positive lookbehind looks like ?.
1 2 3 | $string = 'I live in the yellowhouse'; if(preg_match("/(?<!white)house/i", $string)) |
So we are trying to not match house preceded by white. And guess what, we find it isn’t. This time the string is different you see
Thats right, we may want to know if something is behind something else… And a positive lookbehind looks like ?<=.
1 2 3 | $string = 'I live in the whitehouse'; if(preg_match("/(?<=white)house/i", $string)) |
So we are trying to match house preceded by white. And guess what, we find it!
We have successfully worked out what to do when we don’t want matches to happen. But what about when we do..?
$string = 'A simple string is often short, eg: this.'; if(preg_match("/eg+(?=:)/", $string, $match))
Can we find eg followed by a colon? I think so, hence the match.
So, we look ahead to find matches, negative and positive…
This tutorial is negative…
A negative look ahead which is denoted with ?!. This is useful for checking if a pattern is not in front of the match we wish.
1 2 3 4 | $string = 'I hope you like dogfood!'; // Try to match dog not followed by food if(preg_match("/dog+(?!food)/i", $string)) |
And it should not match
Now lets try a simple search and replace again, this time with an array and the e modifier!
1 2 3 4 5 6 7 | $string = '_GOOD_ is better than _BAD_'; $replacements = array("GOOD" => "Google", "BAD" => "Ask"); $string = preg_replace("/_(.*?)_/ime", "\$replacements['$1']", $string); echo $string; |
and in real:
Google is better than Ask
Overall, “template” systems can be quickly used in order to ensure that the right data is returned.
A lot of the regex in php can be applied into preg_* functions such as preg_replace!
1 2 3 | $string = 'Yahoo is the best search engine'; $string = preg_replace("/Yahoo/", 'Google', $string); |
And bam:
Google is the best search engine!
Now, lets see it in practice!
Is a possible title for this post as we look at the \U modifier. This just limits how many matches are found:
$string = "foobar foodbar foopbar"; if(preg_match("/foo(.*)bar/U", $string)){ //Found a match }
Here we just want to know that we found it and to repeat searches would be pointless and time consuming.
Remember when we tried to match cat with catalog? Well, now we will try again but with \B.
This means that it must be in a word to match and also at the end…
$string = 'There is a catalog'; echo preg_match("/\Bcat\b/i", $string);
But no! It doesn’t work..
\B requires it to match words in another word with characters before their first character.
If the string was ‘Hello World’ then with \B it WOULD match against rld. This way it is in a word and at the end!
If we are trying to match a string then it will match such as CAT in CATALOG but by using a word boundary we can strictly restrict it to singular words as explained below:
1 2 3 | $string = 'There is a catalog'; echo preg_match("/\bcat\b/i", $string); |
And that should return false or echo 0, take away the \b and you should get it echoing 1.
EvoLve theme by Theme4Press • Powered by WordPress Stormation
Innovation through Creation - Flash | PHP | HTML | XML | CSS | SQL - By Eliott Robson
