如何抓住从外部网站的内容片段,并在我的网站上显示他们呢? (类似于一个RSS feed或其他聚合器一样)。
例如,假设我想显示其他网站的日历项:
其他网站:
<h1>Here's our calendar:</h1>
<div class="calendar_item">
<h2>Boston Marathon</h2>
<p class="date">June 23, 2012</p>
<p class="description">This marathon is 26.2 miles and lots of fun.</p>
</div>
<div class="calendar_item">
<h2>Irish Pub Crawl</h2>
<p class="date">July 17, 2012</p>
<p class="description">Shamrocks and green things are super-fun.</p>
</div>
<div class="calendar_item">
<h2>Tim's Birthday</h2>
<p class="date">August 25, 2012</p>
<p class="description">It's Tim's birthday, yo.</p>
</div>
我的网站:
<h1>Here's a feed of some calendar items from someone else's website:</h1>
<div class="event_title">Boston Marathon</div>
<div class="event_date">June 23, 2012</div>
<div class="event_description">This marathon is 26.2 miles and lots of fun.</div>
<div class="event_title">Irish Pub Crawl</div>
<div class="event_date">July 17, 2012</div>
<div class="event_description">Shamrocks and green things are super-fun.</div>
<div class="event_title">Tim's Birthday</div>
<div class="event_date">August 25, 2012</div>
<div class="event_description">It's Tim's birthday, yo.</div>
这里是(使用MAMP)我已经试过:
<?php
$url = "http://example.com";
$page = curl($url);
$pattern = '%
<h2>(.+?)</h2>
%i';
preg_match($pattern,$page,$matches);
print_r($matches);
?>
...其中打印:
Array ( )
该教程的/ etc。 我认为包括模棱两可的答案,如“尝试卷曲”。 这看起来很简单,但我是一个难倒小白。
在预先感谢球员:)