IEnumerable<char> sequence = "abc".Concat("def");
foreach (var element in sequence)
{
Console.WriteLine(element); // Prints a, then b, then c, then d, then e, then f
}
输出
sequence
regex *仅返回preg_match_all中的最后一个匹配匹配已发生但尚未返回到输出数组如何获取所有URL?
当我尝试使用两个preg_match_all获取
内容然后匹配所有图片但我想只使用一个
时,它有效答案 0 :(得分:2)
您可以使用DOMDocument:
$html = "<p class='images'>
<img src='URL1'>
<img src='URL2'>
<img src='URL3'>
<img src='URL4'>
</p>
<p><img src='ThisNot'></p>
";
$dom = new DOMDocument;
$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('p');
$urls = array();
foreach($tags as $p){
if($p->getAttribute('class') == "images"){
$imgs = $p->getElementsByTagName('img');
foreach($imgs as $img){
$urls[] = $img->getAttribute('src');
}
}
}
print_r($urls);
<强>输出:强>
Array
(
[0] => URL1
[1] => URL2
[2] => URL3
[3] => URL4
)