2012-09-23 21 views
1

我想要file_get_contents数组中的每一个链接。因此,我可以应用preg_match代码,然后匹配检测到的第一个p标签中的前20个字符。File_Get_Contents整个数组

我的代码如下:

$links = array(0 => "http://en.wikipedia.org/wiki/The_Big_Bang_Theory", 1=> "http://en.wikipedia.org/wiki/Fantastic_Four"); 
    print_r($links); 
    $link = implode(", " , $links); 
    $html = file_get_contents($link); 

    preg_match('%(<p[^>]*>.*?</p>)%i', $html, $re); 
    $res = get_custom_excerpt($re[1]); 
    echo $res; 

回答

0

您可以在file_get_contents同时使用一个网址。组合链接将不起作用,您需要循环链接并获取内容。

$links = array(0 => "http://en.wikipedia.org/wiki/The_Big_Bang_Theory", 1=> "http://en.wikipedia.org/wiki/Fantastic_Four"); 
    print_r($links); 

    foreach($links as $link){ 
    $html = file_get_contents($link); 
    preg_match('%(<p[^>]*>.*?</p>)%i', $html, $re); 
    $res = get_custom_excerpt($re[1]); 
    echo $res; 
    } 
0

为什么不使用他们的API?

您仍然可以使用file_get_contents来检索内容,但是您可以决定更适合您需要的格式。

他们的API是记录相当不错,请参阅:http://www.mediawiki.org/wiki/API:Main_page

网址会变成什么