How to get IMG tag code from HTML document?

前端 未结 1 1759
醉梦人生
醉梦人生 2021-01-16 23:43

how I get the img code from a text? Now I get the code and URL if the tag looks like: text text , but if the code

1条回答
  •  悲&欢浪女
    2021-01-17 00:30

    Don’t try to parse HTML with regular expressions; use an HTML parser like PHP’s DOM library or the PHP Simple HTML DOM Parser instead (see Gordon’s comment for further alternatives).

    Here’s an example with the PHP Simple HTML DOM Parser:

    $html = str_get_html('…');
    foreach ($html->find('img[src]') as $img) {
        echo $img->getAttribute('src');
    }
    

    0 讨论(0)
提交回复
热议问题