Forum Moderators: coopster
What I did was I exploded the whole string into an array and loop through it and check if the chunk starts with "<img>" and ends with "</img>". But this process is so slow specially if a user put a lot of remarks on their post.
Is their a better and faster way how to do it ?
Thanks.
It sounds as if you are appending each column to a string value first, then analyzing the string. Perhaps it would be faster to analyze each column as you process it within the loop rather than append it to one very large string. Pseudocode ...
$imgs = array(); // initialize
$pattern = <your pattern here, with subpattern capturing parenthesis>;
while (looping through sql results) {
if preg_match_all($pattern, $row['columnWithPossibleImgs'], $matches) {
foreach ($matches[1] as $m) { // $matches[1] represents subpattern captured
$imgs[] = $m;
}
}
}
The preg_match_all pattern will be a great help.
Thanks for the help.
$conn = mysql_connect("$hostname", "$dbUser","$dbUserPw") or die(mysql_error());
mysql_select_db("$dbName", $conn) or die(mysql_error());
$qry="SELECT body FROM smf_messages WHERE id_board=1100";
$ubound=0;
$result=mysql_query($qry,$conn);
echo mysql_num_rows($result);
$imgs = array(); // initialize
$pattern = "/\[img]([^\[]+)\[\/img]/";
while (list($body)=mysql_fetch_row($result)) {
if (preg_match_all($pattern, $body, $matches)) {
foreach ($matches[1] as $m) { // $matches[1] represents subpattern captured
$imgs[$ubound] = $m;
$ubound++;
}
}
}
while ($cnt!=$ubound)
{
$mtmp2=createThumb($imgs[$cnt]) // This line creates a local thumbnail and save the filename in the database
$cnt++;
}
mysql_close($conn);
?>
I have to run the script regularly using cron to scan the board #1100 (SMF) then save the thumbnails and the location of the images in the database. My problem with this code is I don't know if the image URL is still alive and the resources that it will eat on the server once the number of images posted get large.
My purpose in writing this code is to scan board #1100 where users post their images. I inserted a code in the boardindex that display a few thumbnails that links on its original location everytime the page loads .
Any suggestions to improve it ?
Thanks again.
The book Webbots, Spiders, and Screen Scrapers will tell you pretty much exactly what you need to do. It has a specific spider that is used to harvest images too. I haven't used that specific one but I've created a few others from the book and they're pretty great.