大家好我在下面显示了很多数据集(在$ code变量中)。我想知道我如何输出图像的所有值alt =" ..." ?例如,我想得到:音乐俱乐部(太阳)音乐09
从:
<img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
数据样本集:
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">
<div class="portfolio-item">
<a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >
<span class="portfolio-item-hover"></span>
<span class="fullscreen"><i class="icon-play"></i></span>
<img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
</a>
<div class="portfolio-item-title">
<a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>
</div>
<div class="clearfix"></div>
</div>
</div>
答案 0 :(得分:1)
如果您有像下面这样的HTML代码,那么您可以这样做
$re = '/(alt)=("[^"]*")/';
$str = '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">\n <div class="portfolio-item">\n <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >\n <span class="portfolio-item-hover"></span>\n <span class="fullscreen"><i class="icon-play"></i></span>\n <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />\n </a>\n <div class="portfolio-item-title">\n <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>\n </div>\n <div class="clearfix"></div>\n </div>\n </div>';
preg_match_all($re, $str, $matches);
输出:
[0] => Array
(
[0] => alt="Music Club ( sun ) Music 09"
)
[1] => Array
(
[0] => alt
)
[2] => Array
(
[0] => "Music Club ( sun ) Music 09"
)
答案 1 :(得分:1)
如果您真的想使用preg_match_all
,可以使用以下代码。
<?php
$str = <<<END
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">
<div class="portfolio-item">
<a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >
<span class="portfolio-item-hover"></span>
<span class="fullscreen"><i class="icon-play"></i></span>
<img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
</a>
<div class="portfolio-item-title">
<a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>
</div>
<div class="clearfix"></div>
</div>
</div>
END;
preg_match_all('/<img(.*?)alt=\"(.*?)\"(.*?)>/si', $str, $out, PREG_SET_ORDER);
//see first output
var_dump($out);
/*
array(1) {
[0]=>
array(4) {
[0]=>
string(105) "<img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />"
[1]=>
string(65) " src="http://www.example.com/teststorage/episodes/11224/201.jpg" "
[2]=>
string(27) "Music Club ( sun ) Music 09"
[3]=>
string(2) " /"
}
}
*/
//clean array
$alt = array();
foreach($out as $val) {
$alt[] = $val[2];
}
//see cleaned output
var_dump($alt);
/*
array(1) {
[0]=>
string(27) "Music Club ( sun ) Music 09"
}
*/
?>
如果你想做正确的事情,我会调查simple_html_dom。你可以这样做:
<?php
// Create DOM from URL or file
$html = file_get_html('http://www.example.com/page_i_want_to_spider.php');
// Find all images
foreach($html->find('img') as $element)
echo $element->alt . '<br>';
?>
答案 2 :(得分:1)
使用DOMDocument:
$dom = new DOMDocument();
@$dom->loadHTML($html);
$imgs= $dom->getElementsByTagName('img');
foreach ($imgs as $img) {
if ($img->hasAttribute('alt')) echo $img->getAttribute('alt') . '<br/>';
}