Mengambil Isi “src” dan “alt” dari Tag “img” dengan PHP

Misalkan kita punya suatu file “udah_malem.html” yang isinya sebagai berikut.


blablablablalalalallililili

blablablablalalalallililitu gambar lhoili

itu gambar2 lhoitu gambar3 lho

Kemudian kita punya niat iseng buat nampilin isi dari atribut “src” dan “alt” yang ada di semua tag “img” di file html di atas. Gimana caranya?

Bikin file PHP, misalnya kita kasi nama “tes.php”, isinya :

<?php
$html = file_get_contents("udah_malem.html");
preg_match_all('/]+>/i',$html, $tmp1);
$tmp2 = array();
$idx = 0;
foreach ($tmp1[0] as $val) {
preg_match_all('/(alt|src)="([^"]*)"/i',$val, $tmp2[$idx]);
$idx++;
}
foreach ($tmp2 as $key => $value) {
echo $value[2][0]." | ".$value[2][1]."";
}
?>

Kemudian jalankan file “tes.php”, niscaya output pada browser akan nampak seperti berikut.

oi/ini/gambar.jpg | itu gambar lho
oi/ini/gambar2.jpg | itu gambar2 lho
oi/ini/gambar3.jpg | itu gambar3 lho

Referensi : http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>