Using regular expressions to extract content
PHP provides a number of really neat regular expression functions. You can find the list of the regex function at the PHP site.
But the one that I’ve had most fun with is the preg_match_all() function which I’ve been using to do content extraction from an HTML page.
I’m not going to explain what Regular Expression (regex) is in this post. There are whole books on just this one topic along; I would be crazy to think I can explain it all in just a few paragraphs. But in order for you to understand how to use the regex functions you need to have a basic understanding of regular expressions.
If you think back to your childhood days, you would remember a toy that you can match holes with shapes with the corresponding blocks – like the picture here. Well, regular expressions is very much like that toy, but instead you have define your own ’shape’ (or pattern as it’s known) and apply your content to it. Any text that matches the pattern will ‘fall’ through it.
Let’s say you have a block of text like below and you want to extract out the all links from, you can use preg_match_all to do just that.
1 2 3 4 5
$content = "He's goin' everywhere, <a href=\"http://www.bjmckay.com\">B.J. McKay</a> and his best friend Bear. Rollin' down to <a href=\"http://www.dallas.net\">Dallas</a>, who's providin' my palace, off to New Orleans or who knows where."
The pattern you want to look for would be the link anchor pattern, like <a href=”(something)”>(something)</a>. The actual regular expression might look something like
$regex_pattern = "/<a href=\"(.*)\">(.*)<\/a>/";
Once you have your pattern you apply the $content and $regex_pattern to preg_match_all() like this
preg_match_all will store all the matches into the array $matches, so if you output the array, you’ll see something like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Array (  => Array (  => <a href="http://www.bjmckay.com">B.J. McKay</a>  => <a href="http://www.dallas.net">Dallas</a> )  => Array (  => http://www.bjmckay.com  => http://www.dallas.net )  => Array (  => B.J. McKay  => Dallas ) )
From this array, $matches, you should be able to loop through and get the information you need.
I hope this has been useful to you. I know it doesn’t cover all the things this function can do, but for first-timers, it should be a simple look at a very powerful PHP function.
Incidently, PHP also provides the function preg_match(). The difference is preg_match() only matches a single instance of the pattern, whereas preg_match_all() tries to find all matching instances within the content.
In: PHP Tutorials · Tagged with: php basics, sample codes, tutorial