tags:

views:

35

answers:

2

I've never done anything like this, so I'm not really sure what kind of tools within PHP i need to make this happen.

I'm making a list of "recent news items" (just headlines) that'll show up on the side of the site, and would like to be able to look for the first image in a news post (I assume it has something to do with looking for an img tag and loading the src, but I'm not really sure how to do that.

After i do that, i want to be able to resize that image to a thumbnail (like 30x30).

The news posts are stored in a MySQL database, and is being pulled in a variable $newsPost.

Thanks for any help.

+1  A: 

You'll need a HTML parser. Descend to the container of the news post, then look for the img within it. Then grab the image and use the GD functions to resize.

Ignacio Vazquez-Abrams
+1  A: 

You could use regular expressions.

<?php
preg_match("/<img\ssrc\=['"]?(.+)['"]?>/", $newsPost, $matches);
$imgUrl = $matches[1];

echo "<img src='$imgUrl' width='30'>";
mwhite