<?
/**
* Klasa DictTranslator - tłumaczenie polsko-angielski/angielsko-polski korzystając z dict.pl
*
* @author Arkadiusz Maliński :: <hide@address.com>
* @version 0.1.0
*
*/
define('DICT_ANY', 1);
define('DICT_ALL', 2);
class DictTranslator {
private $_searched;
private $_results = array();
/**
* funkcja przeprowadzająca tłumaczenie
*
* @param string słowo bądź słowa do tłumaczenia
* @param int typ tłumaczenia; DICT_ANY - dowolne wyrażenie, DICT_ALL - angielska sentencja
*
* @return array tablica zawierająca pary "polskie wyrażenie" => "angielskie wyrażenie"
*/
function translate($words, $typ = DICT_ANY) {
$arTmpResults = array();
$this->_searched = urlencode($words);
$_dictPage = curl_init();
switch($typ) {
case DICT_ALL:
curl_setopt($_dictPage, CURLOPT_URL, "http://www.dict.pl/plen?words=".$this->_searched."&lang=PL");
break;
case DICT_ANY:
curl_setopt($_dictPage, CURLOPT_URL, "http://www.dict.pl/plen?word=".$this->_searched."&lang=PL");
break;
}
curl_setopt($_dictPage, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_dictPage, CURLOPT_HEADER, 0);
$tmpResult = preg_split('/\n/', curl_exec($_dictPage));
curl_close($_dictPage);
foreach($tmpResult as $line) {
if(preg_match("/<!--m-->/", $line))
array_push($arTmpResults, strip_tags($line));
}
for($i=0; $i<count($arTmpResults); $i=$i+2) {
$this->_results = array_merge($this->_results, array($arTmpResults[$i]=>$arTmpResults[$i+1]));
}
return $this->_results;
}
}
?>