09/10/2018, 23:33
Tách file ảnh trong code html
Hi, có bạn nào biết tách ảnh từ tag <img> trong 1 đoạn code html không, giúp mình với.
Input: - đoạn code html.
Output: - danh sách các ảnh có trong nội dung file html từ tag <img >
Cảm ơn rất nhiều.
ps: dùng PHP script
Input: - đoạn code html.
Output: - danh sách các ảnh có trong nội dung file html từ tag <img >
Cảm ơn rất nhiều.
ps: dùng PHP script
Bài liên quan
string GetFirstImageInContent(string content)
{
content = content.Replace("<IMG","<img");
int ifound = 1111;
while(ifound > -1)
{
string oldstr = "";
if(ifound != 1111)
for(int i=ifound; i < content.Length; i ++)
{
if(content.Substring(i,1).Equals(">"))
{
oldstr = content.Substring(ifound,i - ifound + 1);
int isrc1 = oldstr.IndexOf("src=\"");
int isrc2 = -1;
for(int j = isrc1 + 5; j < oldstr.Length ; j ++)
{
if(oldstr.Substring(j,1).Equals("\""))
{isrc2 = j;break;}
}
string str_src = oldstr.Substring(isrc1 + 5,isrc2 - isrc1 - 5);
return str_src;
}
}// end for
ifound = content.IndexOf("<img ");
}
return "";
}
<?php
$htmlcode = 'TD><IMG src="vnn2_files/images1_chaphanh.jpg" width=400 border=0>
</TD></TR>TD><IMG src="vnn2_files/images2_chaphanh.jpg" width=400 border=0>
</TD></TR>TD><IMG src="vnn2_files/images3_chaphanh.jpg" width=400 border=0>
</TD></TR>TD><IMG src="vnn2_files/images4_chaphanh.jpg" width=400 border=0> </TD></TR>';
str_replace('<img','<IMG',$htmlcode);
preg_match_all('#<IMG(.*?)src="(.*?)"#', $htmlcode, $link);
print_r($link***91;2***93;);
?>
Mà sao cái code trên kia lại replace img thành IMG nhỉ, bỏ thêm cái modifier /i là được rồi, tự nhiên thừa thêm 1 bước xử lý