gpt4 book ai didi

php - 从 mysql 查询中提取第一个图像标签

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:20 25 4
gpt4 key购买 nike

我想知道如何从 mysql 查询中提取第一个图像 URL。

举个例子:

<p>Nowadays forums have become very popular, almost every site on the 
web have a forum. An Internet forum, bulletin board or message board, is
an online discussion site where people can hold conversations in the
form of posted messages.</p>
<p>This is a list of top 10 free and open source software for creating your own forum.</p>
<h3>1. phpBB</h3>
phpBB is a free flat-forum bulletin board software solution that can
be used to stay in touch with a group of people or can power your entire
website.<br><br><img alt="" src="http://www.yoursite.com/phpbb.jpg" align="none"><br><br><h3>2. Simple Machines Forum</h3>
SMF is a free, professional grade software package that allows you to set up your own online community within minutes.<br><br><img alt="" src="http://www.yoursite.com/smf.jpg" align="none"><br>

我需要从中提取 http://www.yoursite.com/phpbb.jpg

我该怎么做。提前致谢

最佳答案

$text = '
<p>Nowadays forums have become very popular, almost every site on the
web have a forum. An Internet forum, bulletin board or message board, is
an online discussion site where people can hold conversations in the
form of posted messages.</p>
<p>This is a list of top 10 free and open source software for creating your own forum.</p>
<h3>1. phpBB</h3>
phpBB is a free flat-forum bulletin board software solution that can
be used to stay in touch with a group of people or can power your entire
website.<br><br><img alt="" src="http://www.yoursite.com/phpbb.jpg" align="none"><br><br><h3>2. Simple Machines Forum</h3>
SMF is a free, professional grade software package that allows you to set up your own online community within minutes.<br><br><img alt="" src="http://www.yoursite.com/smf.jpg" align="none"><br>
';

$doc = new DOMDocument();
$doc->loadHTML($text);
$xpath = new DOMXPath($doc);
$images = $xpath->query("//img");
$first_image = $images->item(0);
$firstsrc = $first_image->attributes->getNamedItem('src')->nodeValue;
echo $firstsrc;

输出:

http://www.yoursite.com/phpbb.jpg

更新:@uınbɐɥs 的另一个简短回答:

$doc = DOMDocument::loadHTML($text);
$firstsrc = $doc->getElementsByTagName('img')->item(0)->getAttribute('src');
echo $firstsrc;

关于php - 从 mysql 查询中提取第一个图像标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15749031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com