使用正则表达式从HTML代码中提取第一个图像源? [英] Using regular expressions to extract the first image source from html codes?

查看:83
本文介绍了使用正则表达式从HTML代码中提取第一个图像源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设:有很多包含表格,div,图片等的html代码。

>

问题:我如何获得所有发生的匹配。更重要的是,如何获得img标签源(src =?)。



示例:

 < img src =http:/ /example.com/g.jpgalt =/> 

如何打印 http://example.com/g.jpg 在这种情况下。我想假设在我提到的HTML代码中还有其他标签,并且可能不止一个图像。是否有可能在html代码中有所有图像源的数组?



我知道这可以通过正则表达式实现,但我无法获得

非常感谢您的帮助。

>虽然正则表达式可以适用于各种各样的任务,但我发现它在解析HTML DOM时通常很短。 HTML的问题在于你的文档结构变化很大,以至于很难精确地(并且准确地说,我的意思是100%的成功率并且没有误报)提取一个标签。

我推荐你做的是使用DOM解析器,比如 SimpleHTML 并使用它:

  function get_first_image($ html) {
require_once('SimpleHTML.class.php')

$ post_html = str_get_html($ html);

$ first_img = $ post_html-> find('img',0);

if($ first_img!== null){
return $ first_img-> src;
}

返回null;
}

有人可能会认为这太过矫枉过正,但最终会更容易维护并且允许更多的可扩展性。例如,使用DOM解析器,我也可以得到alt属性。



可以设计一个正则表达式来实现相同的目标,但会受到这样的限制:它会强制 alt 属性位于 src 之后或相反,并且克服这个限制会增加复杂性到正则表达式。



另外,请考虑以下内容。使用正则表达式正确地匹配< img> 标记并仅获取 src 属性(在组2中捕获),您需要以下正则表达式:

 < \ * * img \ s + [^>] * ?\ s * src \s * = \s *(['])((\\?+。)*?)\1 [^>]??> 

然后再次,上述可能会失败,如果:




  • 属性或标签名称以大写字母开头,并且不使用 i 修饰符。

  • 不在 src 属性周围使用引号。

  • 另一个属性为 src 在其值的某处使用> 字符。

  • 我没有预见到的其他原因。



再说一遍,不要使用正则表达式来解析一个dom文档。





$ b

编辑:如果您想要所有图片:

 功能g et_images($ html){
require_once('SimpleHTML.class.php')

$ post_dom = str_get_dom($ html);

$ img_tags = $ post_dom-> find('img');

$ images = array();

foreach($ img_tags as $ image){
$ images [] = $ image-> src;
}

返回$ images;
}


I would like to know how this can be achieved.

Assume: That there's a lot of html code containing tables, divs, images, etc.

Problem: How can I get matches of all occurances. More over, to be specific, how can I get the img tag source (src = ?).

example:

<img src="http://example.com/g.jpg" alt="" />

How can I print out http://example.com/g.jpg in this case. I want to assume that there are also other tags in the html code as i mentioned, and possibly more than one image. Would it be possible to have an array of all images sources in html code?

I know this can be achieved way or another with regular expressions, but I can't get the hang of it.

Any help is greatly appreciated.

解决方案

While regular expressions can be good for a large variety of tasks, I find it usually falls short when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately (and by accurately I mean 100% success rate with no false positive) extract a tag.

What I recommend you do is use a DOM parser such as SimpleHTML and use it as such:

function get_first_image($html) {
    require_once('SimpleHTML.class.php')

    $post_html = str_get_html($html);

    $first_img = $post_html->find('img', 0);

    if($first_img !== null) {
        return $first_img->src;
    }

    return null;
}

Some may think this is overkill, but in the end, it will be easier to maintain and also allows for more extensibility. For example, using the DOM parser, I can also get the alt attribute.

A regular expression could be devised to achieve the same goal but would be limited in such way that it would force the alt attribute to be after the src or the opposite, and to overcome this limitation would add more complexity to the regular expression.

Also, consider the following. To properly match an <img> tag using regular expressions and to get only the src attribute (captured in group 2), you need the following regular expression:

<\s*?img\s+[^>]*?\s*src\s*=\s*(["'])((\\?+.)*?)\1[^>]*?>

And then again, the above can fail if:

  • The attribute or tag name is in capital and the i modifier is not used.
  • Quotes are not used around the src attribute.
  • Another attribute then src uses the > character somewhere in their value.
  • Some other reason I have not foreseen.

So again, simply don't use regular expressions to parse a dom document.


EDIT: If you want all the images:

function get_images($html){
    require_once('SimpleHTML.class.php')

    $post_dom = str_get_dom($html);

    $img_tags = $post_dom->find('img');

    $images = array();

    foreach($img_tags as $image) {
        $images[] = $image->src;
    }

    return $images;
}

这篇关于使用正则表达式从HTML代码中提取第一个图像源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆