来自新闻网站,我们有许多标签,如:
<a class="picLink" target="_blank" href="/EN/news/397423/MY-TEST-NEWS">
<img class="fr" width="115" style="margin:6px 0px 0px 8px;width: 115px;" src="http://sample.com/files/EN/news/369326_276.jpg" alt="MY TEST NEWS !">
</a>
我想从中获取href
和图片src
以及alt
属性值。
我的简单代码不正确:
<meta charset='UTF-8' />
<?php
error_reporting(1);
$handle='http://sample.com';
$handle = file_get_contents($handle);
preg_match_all('/<a[^>]+class="titr1y"[^>] href="(.*?)"*><img class="[^>]" width="[^>]" style="[^>]" src="(.*?)" alt="(.*?)"*><\/a>/si', $handle, $matching_data);
print_r($matching_data);
?>
请帮我纠正。
答案 0 :(得分:4)
你可以用这个来获得它
$re = '/(alt|href|src)=("[^"]*")/';
$str = '<a class="picLink" target="_blank" href="/EN/news/397423/MY-TEST-NEWS">\n <img class="fr" width="115" style="margin:6px 0px 0px 8px;width: 115px;" src="http://sample.com/files/EN/news/369326_276.jpg" alt="MY TEST NEWS !">\n</a>\n<a class="picLink" target="_blank" href="/EN/news/397423/MY-TEST-NEWS">\n <img class="fr" width="115" style="margin:6px 0px 0px 8px;width: 115px;" src="http://sample.com/files/EN/news/369326_276.jpg" alt="MY TEST NEWS !">\n</a>';
preg_match_all($re, $str, $matches);
print_r($matches);
<强>输出强>
(
[0] => Array
(
[0] => href="/EN/news/397423/MY-TEST-NEWS"
[1] => src="http://sample.com/files/EN/news/369326_276.jpg"
[2] => alt="MY TEST NEWS !"
[3] => href="/EN/news/397423/MY-TEST-NEWS"
[4] => src="http://sample.com/files/EN/news/369326_276.jpg"
[5] => alt="MY TEST NEWS !"
)
[1] => Array
(
[0] => href
[1] => src
[2] => alt
[3] => href
[4] => src
[5] => alt
)
[2] => Array
(
[0] => "/EN/news/397423/MY-TEST-NEWS"
[1] => "http://sample.com/files/EN/news/369326_276.jpg"
[2] => "MY TEST NEWS !"
[3] => "/EN/news/397423/MY-TEST-NEWS"
[4] => "http://sample.com/files/EN/news/369326_276.jpg"
[5] => "MY TEST NEWS !"
)
)
数组[2]中的您将获得所需的值