hi all i got many sets of data show below(in $code variable). i wonder how i can output all values of image alt="..." ? For example i want to get : Music Club ( sun ) Music 09
from:
<img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
sample set of data:
<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>
If you realy want to use preg_match_all
you could use the following code.
<?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"
}
*/
?>
If you want to do the right thing however, I would look into simple_html_dom. You can do something like:
<?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>';
?>