iT邦幫忙

0

PHP正規表達式的問題

php

使用php抓取無名分類相簿的資料

網址如下
http://www.wretch.cc/album/?func=hot&hid=0&class_id=9

目前要抓的資料是中間4x5的圖片、網址、名稱

下面為原始碼大概的資料

<li>
<div class="image_link">
<a href="http://tw.rd.yahoo.com/referurl/wretch/album/score/0/9/*http://www.wretch.cc/album/album.php?id=babyeri&book=63" target="_blank"><img src="http://f4.wretch.yimg.com/babyeri/63/thumbs/t1834944605.jpg" alt="E˙宅" /><cite></cite></a>
</div>
<div class="grid">
<a href="http://tw.rd.yahoo.com/referurl/wretch/album/score/0/9/*http://www.wretch.cc/album/album.php?id=babyeri&book=63" target="_blank">babyeri</a>
<strong>E˙宅</strong>

</div>
</li>
<li>
<div class="image_link">
<a href="http://tw.rd.yahoo.com/referurl/wretch/album/score/0/9/*http://www.wretch.cc/album/album.php?id=youpkcs&book=16" target="_blank"><img src="http://f9.wretch.yimg.com/youpkcs/16/thumbs/t1249649407.jpg" alt="男人看了會跳起來" /><cite></cite></a>
</div>
<div class="grid">
<a href="http://tw.rd.yahoo.com/referurl/wretch/album/score/0/9/*http://www.wretch.cc/album/album.php?id=youpkcs&book=16" target="_blank">youpkcs</a>

<strong>男人看了會跳起來</strong>
</div>
</li>

如果使用preg_match_all那麼正規表達式該如何下才能抓取這些資料呢?

並將這20筆資料各別代出

謝謝

wordsmith iT邦高手 1 級 ‧ 2008-10-03 15:16:59 檢舉
如果沒有限定一定要用正規表示法來抓的話,例用dom的方式取值,也許更容易一點。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

14
brianc
iT邦研究生 1 級 ‧ 2008-10-06 11:51:20
最佳解答
&lt;pre class="c" name="code">
$html = file_get_contents('http://www.wretch.cc/album/?func=hot&hid=0&class_id=9');
$pattern = '|&lt;a href="(.*)" target="_blank">&lt;img src="(.*)" alt="(.*)" />&lt;cite>&lt;/cite>&lt;/a>|';
preg_match_all($pattern, $html, $matches);
print_r($matches);

我要發表回答

立即登入回答