使用file_get_contents从其他网站获取数据

时间:2015-11-29 23:04:00

标签: php extract file-get-contents

http://leagueoflegends.wikia.com/wiki/V1.0.0.101?action=edit 这是我试图从中获取数据的网站。此数据可免费使用,不受版权保护。 这就是我现在所做的,但它似乎并没有起作用:

$leagueoflegendswebsite = file_get_contents($_POST['leagueoflegendswebsite']);
    $first_step = explode( '<textarea>' , $leagueoflegendswebsite );
    $second_step = explode("</textarea>" , $first_step[0] );
    echo $second_step[1];

例如,我现在想从这个网站上获得冠军的名字

{{ci|Sona|Sona, the Maven of the Strings}}


{{ci|Blitzcrank}}

这将是Sona和Blitzcrank并且有一种简单的方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:0)

$leagueOfLegendsWebsiteHtml = file_get_contents("http://leagueoflegends.wikia.com/wiki/V1.0.0.101?action=edit");

$matches = array();
preg_match_all('/{{ci\|([^\|||}]+)/', $leagueOfLegendsWebsiteHtml, $matches);

print_r($matches);

应该使用regexp获取名称。

结果是:

Array
(
    [0] => Array
        (
            [0] => {{ci|Sona
            [1] => {{ci|Sona
            [2] => {{ci|Blitzcrank
            [3] => {{ci|Fiddlesticks
            [4] => {{ci|Garen
            [5] => {{ci|Gragas
            [6] => {{ci|Jax
            [7] => {{ci|Karthus
            [8] => {{ci|Kennen
            [9] => {{ci|Kassadin
            [10] => {{ci|Katarina
            [11] => {{ci|Kog'Maw
            [12] => {{ci|Rammus
            [13] => {{ci|Shaco
            [14] => {{ci|Singed
            [15] => {{ci|Taric
            [16] => {{ci|Twitch
            [17] => {{ci|Vladimir
            [18] => {{ci|Zilean
            [19] => {{ci|Ebonmaw
        )

    [1] => Array
        (
            [0] => Sona
            [1] => Sona
            [2] => Blitzcrank
            [3] => Fiddlesticks
            [4] => Garen
            [5] => Gragas
            [6] => Jax
            [7] => Karthus
            [8] => Kennen
            [9] => Kassadin
            [10] => Katarina
            [11] => Kog'Maw
            [12] => Rammus
            [13] => Shaco
            [14] => Singed
            [15] => Taric
            [16] => Twitch
            [17] => Vladimir
            [18] => Zilean
            [19] => Ebonmaw
        )

)
相关问题