如何从多张图片中获取第一张图片的链接


how get link on first image from several images?

我有代码:

$images = '
<img src="./243234345/Desert1.jpg" alt="" width="1024" height="768" />
<img src="./243234345/Desert2.jpg" alt="" width="300" height="100" />
<img src="./243234345/Desert3.jpg" alt="" width="500" height="120" />
<img src="./243234345/Desert4.jpg" alt="" width="100" height="50" />
';

请告诉我如何获得第一张图片上的链接?

您可以使用preg_match来执行此操作。

$images = '
<img src="./243234345/Desert1.jpg" alt="" width="1024" height="768" />,
<img src="./243234345/Desert2.jpg" alt="" width="300" height="100" />,
<img src="./243234345/Desert3.jpg" alt="" width="500" height="120" />,
<img src="./243234345/Desert4.jpg" alt="" width="100" height="50" />
';
$matches = array();
preg_match('/<img's*src's*='s*[''"]([^''"]*)[''"]/',$images,$matches);
echo $matches[1];

你能试着使用像, 这样的分隔符吗

$images = '
<img src="./243234345/Desert1.jpg" alt="" width="1024" height="768" />
<img src="./243234345/Desert2.jpg" alt="" width="300" height="100" />
<img src="./243234345/Desert3.jpg" alt="" width="500" height="120" />
<img src="./243234345/Desert4.jpg" alt="" width="100" height="50" />
';
$imagesArray = explode('/>',trim($images)); 
$FirstImage =  $imagesArray[0];// display first image
$FirstImage =  $FirstImage .'/>';
preg_match( '@src="([^"]+)"@' , $FirstImage , $matchData );
print_r($matchData);

你可以试试这个:http://simplehtmldom.sourceforge.net/