使用php从html img标签获取图像url


geting image url from a html img tag, using php

html中并非所有src属性,div ID以"post_message_"开头

HTML代码

<div id="another_div">
 <img src="http://domain.com/folder/somfilename.jpg" border="0" alt="">
</div>
<div id="post_message_412">
 <img src="http://domain.com/folder/filename1.jpg" border="0" alt="">
</div>
<div id="post_message_413">
 <img src="http://domain.com/folder/filename2.jpg" border="0" alt="">
</div>
<div id="post_message_414">
 <img src="http://domain.com/folder/filename3.jpg" border="0" alt="">
</div>
<div id="post_message_415">
 <img src="http://domain.com/folder/filename4.jpg" border="0" alt="">
</div>

使用php preg_match((、preg_match_all((或preg_split((或任何其他可能的解决方案,在div ID中获取image标记的url以"post_message_"(与"another_div"ID名称不同(开头。

之后,我想从数组中获取url

假设HTML与示例中的结构相同,则可以使用:<div's*id="post_message_[^"]+">'s*<img src="([^"]+)

它之所以长,只是因为文字。

  • 's*匹配0+个空白字符
  • [^"]+匹配非"的字符