2012-10-02 80 views
1

我如何完全匹配img标签的多个实例?我读了一些关于preg_match的教程,但从未真正理解。pregmatch多个来源

我有这个作为我的基地:

<img src="http://example.com/1.png" alt="Example" /> 

<img class="Class" src="http://example.com/2.jpg" alt="Example 2" /> 

而且我做了一个小的,如正则表达式:

<img (src="|class="Class" src=")http://.+\.(?:jpe?g|png)" alt=" 

在此之后,我卡住了。我如何继续匹配所有,直到两个字符串的结尾?

我发现了关于PHP网站本身的阵列部分:

preg_match('@^(?:http://)?([^/]+)@i', 
    "http://www.php.net/index.html", $matches); 
$host = $matches[1]; 

使用我的代码,我如何获得图像URL,以及ALT标签?

谢谢!

+0

匹配'从多个的img标签src'属性,使用['preg_match_all'](http://www.php.net/manual/en /function.preg-match-all.php) – air4x

回答

1

对于原始问题,请使用preg_match_all()函数来获取所有匹配项。

对于第二个问题(“使用我的代码,我该如何获取图像URL和ALT标记?”),基本上你的正则表达式是正确的。但是,我建议首先得到整个<img>标签,然后再做另一个preg_match()以获得hrefalt属性,因为它们的顺序可能会有所不同。

$html = "<img src='test.jpg' alt='aaaaaaaaaaa!'> adfa <img src='test2.jpg' alt='aaaaaaaaaaa2'> "; 

$pattern = '/<img\s[^>]*>/'; 
$count = preg_match_all($pattern, $html, $matches, PREG_SET_ORDER); 

echo "Found: " . $count . "\n"; 
if ($count > 0) { 
    foreach ($matches as $match) { 
     $img = $match[0]; 
     echo "img: " . $img . "\n"; 
     if (preg_match("/src=['\"]([^'\"]*)['\"]/", $img, $val)) { # UPDATE: use() to catch the content of src 
      $src = $val[1];  # UPDATE: get the part in() 
     } 
     if (preg_match("/alt=['\"]([^'\"]*)['\"]/", $img, $val)) { # UPDATE 
      $alt = $val[1];  # UPDATE 
     } 

     echo "src = " . $src . ", alt = " . $alt . "\n"; 
    } 
} 

UPDATE

回答您的评论。 当然。只需在src=之后使用一组来捕捉零件。 我更新了上面的源代码并评论了“更新”。

+0

谢谢!这工作!然而,结果仍然由文本'src =“'。是否有可能在preg_match本身中删除它们?或者我必须使用'str_replace'或其他手动删除它们?因为我之前见过的代码img链接直接从preg_match。感谢! – MrYanDao

1

为什么不是DOMDocument?你可以得到所有的属性无论怎样的图像都写入:

$string = '<img class="Class" src="http://example.com/2.jpg" alt="Example 2" />'; 

$dom = new DOMDocument; 
$dom->loadHTML($string); 
$xpath = new DOMXPath($dom); 

$query = '//img'; 
$elements = $xpath->query($query); 

$attributes = array(); 
$i = 0; 
foreach($elements as $one){ 
    foreach($one->attributes as $att){ 
     $attributes[$i][$att->nodeName] = $att->nodeValue; 
    } 
    $i++; 
} 
print_r($attributes); 

/*Array 
(
    [0] => Array 
     (
      [class] => Class 
      [src] => http://example.com/2.jpg 
      [alt] => Example 2 
     ) 

)*/ 
+0

感谢您!但是,我不是很擅长'DomDocument',所以我想我只会坚持正常。谢谢! – MrYanDao

+0

正则表达式在HTML中不正常。 :) –