问题描述:

I am writing a PHP script to iterate through a directory of PHP web pages and pull out the first image from each of those pages and print HTML for the image. I get an error and I'm not sure how to fix it.

Error:

Notice: Undefined offset: 0 in MYPC\scripts\summary.php on line 27

Iterate through directory:

$dir = 'posts/*';

foreach(glob($dir) as $file) {

$contents = file_get_contents($file);

page_title($file);

summarise($contents);

catch_that_image($file);

}

Find first image (part with error):

function catch_that_image( $str ) {

$output = preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $str, $matches);

echo $matches[1][0];

}

相关阅读:
Top