Category Archives: Coding

Regular Expression to Get All Image URLs From a Page

Have you ever wanted to, while working on some sort of PHP project, get an array listing of all the images used in a chunk of HTML? I’ve been planning out a web app over the past couple months, which will be doing a bit of RSS parsing, and I thought it would be nice to do just that, when it came time to start coding. Suppose you were going to show a summary of an article from a feed, with a link to the original source. Wouldn’t it look better if you pulled an image from that article, scaled it down if necessary, and displayed it next to it? (Caching it, of course. Hotlinking == bad.)

I was reading an article from Cats Who Code and, lo and behold, there was a code snippet that did just that with a regular expression. (I decided to file it away to save time in the future.)

$images = array();
preg_match_all('/(img|src)=("|')[^"'>]+/i', $data, $media);
unset($data);
$data=preg_replace('/(img|src)("|'|="|=')(.*)/i',"$3",$media[0]);
foreach($data as $url)
{
$info = pathinfo($url);
if (isset($info['extension']))
{
if (($info['extension'] == 'jpg') ||
($info['extension'] == 'jpeg') ||
($info['extension'] == 'gif') ||
($info['extension'] == 'png'))
array_push($images, $url);
}
}

Source: 15 PHP regular expressions for web developers

Feed-parsing is an excellent use for this, as you have just the article, no layout-related imagery, like you would see if you were screen-scraping a web page to obtain the image URLs. Though I imagine Digg takes the latter route when they dig-up (Freudian pun unintended, honest) the thumbnails that go along with their news links.

Getting RSS and Twitter Subscriber Counts in WordPress

I previously wrote a post about how some blogs are displaying their RSS subscriber and Twitter follower counts. Mac AppStorm is combining their Twitter and RSS counts into one number, and FreelanceSwitch has a section in their footer with separate readouts for RSS, Twitter,…

Basic HTTP Authentication With PHP

Basic HTTP authentication is rudimentary method of requesting a username and password, then allowing or denying access based on the credentials entered. You’ve probably seen it in action somewhere or another. If you try to subscribe to a protected RSS feed, such as the…

40+ Invaluable PHP Tutorials and Resources

My newest post on Net.Tuts+ was published yesterday: 40+ Invaluable PHP Tutorials and Resources It is a roundup of articles, tutorials and tools of interest to PHP coders, on topics such as security, OOP, frameworks, and WordPress. PHP is one of the most popular…

Transforming Capital Letters With PHP

PHP has some useful functions for dealing with capital letters in string variables. You can make a string all uppercase or lowercase. You can make only the first letter a capital, as in a name, or every other letter a capital for a title…

Securing PHP Web Forms

Chris Coyier has written an interesting article on securing form scripts. Serious Form Security talks about token matching, hack logging, and a few other useful techniques to apply to a form processing script. Token matching is definitely a trick worth learning, since it will…

Getting the Number of Words or Characters With PHP

Need to check the number of characters in a string, perhaps for sending user input to Twitter via the API? It can be done easily by using the strlen() function, which simply returns the number of characters in the string passed to it. $phrase…

PHP Version_Compare()

While I was working on a WordPresss plugin recently, I needed to implement a version check. The plugin required WordPress 2.7 or greater, and it made use of several functions available only in PHP 5 and up (such as SimpleXML). It seemed like there…

Add Power to Your PHP With Multi-Tiered Applications

Net.Tuts+ recently published an interesting article by Jason Lengstorf. It deals with the separation of PHP code into three tiers, database, processing, and display. (Much like the MVC approach to programming.) It might not be immediately obvious, but separating your applications into a tiered…

5 PHP Mistakes and How to Avoid Them

PHP is an easy scripting language to learn, but mastering it is a whole other matter. Here are a few common mistakes that beginners, and even experienced PHP developers, might make once in awhile. They’re not really visible mistakes, such as the ones that…