我正在编写一个从 Wikipedia 获取数据的脚本。
一个常见的问题是例如我想获取:
北史翠柏岛
但是我们正在获取的字符串在下面,所以需要删除废话
[[North Stradbroke Island]]'
当前的抓取代码是:
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,"http://en.wikipedia.org/wiki/Special:Export/" . $wiki['suburb'] . ",_" . $wiki['state'] . "");
curl_setopt($curl_handle,CURLOPT_TIMEOUT,10);
curl_setopt($curl_handle,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$xml = curl_exec($curl_handle);
curl_close($curl_handle);
$x = simplexml_load_string($xml);
$text = $x->page->revision->text;
$arr = explode("| ", $text);
$wikipedia = array();
foreach($arr as $s){
$pair = preg_split('/= /', $s);
$key = substr($pair[0],0,strpos($pair[0]," "));
switch($key){
case "lga":
case "pop":
case "dist1":
$wikipedia[$key] = substr($pair[1],0,-1);
break;
case "near-nw":
case "near-n":
case "near-ne":
case "near-w":
case "near-e":
case "near-sw":
case "near-s":
case "near-se":
$value = $pair[1];
if($value != ""){
$value =substr($pair[1],2,strpos($pair[1],",")-2);
}
$wikipedia[$key] = $value;
break;
}
}
在我的页面上,我有:
<?
$wiki['suburb'] = str_replace(" ", "_", $r['suburb']);
$wiki['state'] = convertStateWiki($r['state']);
include("/path-to-wiki-file/wiki.suburb.php");
if ($wikipedia != NULL){
?>
并回显结果:(示例)
<a href="reviews/<?=strtolower($r['state']);?>/<?=strtolower(str_replace(" ", "-", $wikipedia['near-nw']));?>/"><?=$wikipedia['near-nw'];?></a>
所以本质上是:我们使用 wikis 导出提要,一个郊区。那个郊区,可能已经被输入到维基百科中,比如:
[['一些郊区名称]'] 例如
我需要将以上内容返回为:Some Suburb Name
我们需要去除所有非 ALPHA 字符,我不是 100% 使用 php,所以如果这听起来很愚蠢,请随意说出来。但请不要投反对票,因为我提供了尽可能多的代码。
我只需要阻止返回的数据包含除字母字符以外的任何内容(必须允许空格)