我有代码:
$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" />
';
请告诉我如何在第一张图片上获取链接?
答案 0 :(得分:0)
您可以尝试使用像,
$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);
答案 1 :(得分:0)
您可以使用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];