PHP regex FAQ: Can you share some PHP regex examples using the PHP preg_match
function?
For a recent PHP program I was writing, I experimented with the PHP preg_match
function and some regular expressions, trying to get the behavior I was looking for. Now that they're working properly, I thought I'd share a few simple examples of my "PHP regex" preg_match
examples/experiments.
PHP regex example 1: A string ending in a question mark
In this first PHP regex example I'm trying to match any sentence that ends in a question mark. In this example the "^
" symbol means "beginning of line", the "$
" means "end of line", and the ".*
" part means "any number of any characters":
<? $pattern = "/^.*\?$/"; $string = "this is good???"; $count = preg_match($pattern, $string); print $count; ?>
PHP regex example 2: Non-alphanumeric keys pattern
In this second example I'm looking for every non-alphanumeric keyboard key that can be type:
<? $pattern = "/[`'\"~!@# $*()<>,:;{}\|]/"; $string = "thi|sisatest"; $count = preg_match($pattern, $string); print $count; ?>
Frankly, this probably wasn't a great approach, and I should have reversed the regex search pattern. I think what I was trying to do was only allow alphanumeric characters, and I probably would have been better looking for something like "0-9a-zA-Z", but this is still a decent example for other uses. I'll leave this other pattern approach as an exercise for the reader. :)
PHP regex pattern 3: preg_match for a PHP URI (GET request)
Okay, I just need to do some more preg_match
work for a new PHP program I'm working on, and created the following test script for a preg_match
pattern to test against a URI as part of testing the data I receive in a GET request. Here's the complete test script I just worked up:
<?php # a php uri pattern match (for a php get request) # look for anything other than these allowable characters # A-Z, a-z, 0-9, _, ., -, +, ?, /, = $pattern = "/[^A-Za-z0-9_\.\-\+\?\/=]/"; $string = "?q=foo+a_b+a-z.java/foo/bar.cgi"; if (preg_match($pattern, $string)) { echo "string had bad characters in it"; } else { echo "no bad characters"; } ?>
As you can see from that code, there is a collection of characters I think should be valid in a PHP GET request, so I specify those, and then tell the pattern that anything other than these characters should trigger a match (with the ^
character as the first character in my pattern). In this way I can allow good URI and GET requests through, and reject other URIs, which may be attempts to hack the system.