getting image src in php

后端 未结 4 1542
野趣味
野趣味 2021-01-16 07:17

how to get image source from an img tag using php function.

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 08:05

    Or, you can use the built-in DOM functions (if you use PHP 5+):

    $doc = new DOMDocument();
    $doc->loadHTMLFile($url);
    $xpath = new DOMXpath($doc);
    $imgs = $xpath->query("//img");
    for ($i=0; $i < $imgs->length; $i++) {
        $img = $imgs->item($i);
        $src = $img->getAttribute("src");
        // do something with $src
    }
    

    This keeps you from having to use external classes.

提交回复
热议问题