如果仅以特定语言翻译维基百科文章,如何提取?

时间:2016-11-20 13:52:36

标签: php wikipedia-api

如何将PHP条件添加到以下代码中,以便如果英语维基百科文章具有页面的阿拉伯语版本(通过页面上的链接),那么代码将检索文章的阿拉伯语版本?< / p>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <?php
    $array = array();
$myFile = "C:/Users/Public/english4.txt";

set_time_limit(0);
$array = file($myFile, FILE_IGNORE_NEW_LINES);
for ($x=0; $x<=100; $x++) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?format=xml&prop=extracts&action=query&titles=".$array[$x]."&redirects=true");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Testing for http://stackoverflow.com/questions/8956331/how-to-get-results-from-the-wikipedia-api-with-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

//echo $result;

        $e=htmlspecialchars_decode($result);
        $eb=strip_tags($e);

     $eb=  preg_replace('~[^a-zA-Z]+~', ' ', $eb);

     $output = preg_replace('!\s+!', ' ', $eb);
       $n=str_replace("�","",$output);
       $r=substr_count($n, " ");
      if ($r>100){
      file_put_contents($x.".txt",$n);}

}
?>

0 个答案:

没有答案