1

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

4

4 に答える 4

8

または、組み込みのDOM関数を使用できます(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
}

これにより、外部クラスを使用する必要がなくなります。

于 2010-01-18T14:42:47.413 に答える
5

これを見て検討してください。

これが問題を解決する方法として受け入れられているかどうかはわかりませんが、次のコード スニペットを確認してください。

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images 
foreach($html->find('img') as $element) 
       echo $element->src . '<br>';

// Find all links 
foreach($html->find('a') as $element) 
       echo $element->href . '<br>';
于 2010-01-18T09:38:23.023 に答える
4

PHP Simple HTML DOM Parser ( http://simplehtmldom.sourceforge.net/ )を使用できます。

// Create DOM from URL or file

$html = file_get_html('http://www.google.com/');

// Find all images 

foreach($html->find('img') as $element) {
   echo $element->src.'<br>';
}

// Find all links 

foreach($html->find('a') as $element) {
   echo $element->href.'<br>';
}
于 2010-01-18T09:39:09.917 に答える
1
$path1 = 'http://example.com/index.html';//path of the html page
$file = file_get_contents($path1);
$dom = new DOMDocument;

@$dom->loadHTML($file);
$links = $dom->getElementsByTagName('img');
foreach ($links as $link)
{    
    $re = $link->getAttribute('src');
    $a[] = $re;
}

出力:

Array
(
    [0] => demo/banner_31.png
    [1] => demo/my_code.png
)
于 2013-10-28T11:48:05.943 に答える