Skip to content

Instantly share code, notes, and snippets.

@wgm89
Last active November 24, 2016 02:16
Show Gist options
  • Select an option

  • Save wgm89/1b3a95b8a3a68afee255 to your computer and use it in GitHub Desktop.

Select an option

Save wgm89/1b3a95b8a3a68afee255 to your computer and use it in GitHub Desktop.
中英翻译脚本
#!/usr/bin/php
<?php
//author:saeed
$api = 'http://dict.youdao.com/fsearch?q=';
if(count($argv) > 1 && $argv[count($argv)-1] != '-h'){
$arg = array_shift($argv);
if($arg != 'php'){
$argv = array_slice($argv, 0);
}else{
$argv = array_slice($argv, 1);
}
$word = implode(' ', $argv);
$response = file_get_contents($api.$word);
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $response, $vals);
xml_parser_free($xml_parser);
if(preg_match("/[a-z]/i", $word[0])){
$tag = 'CONTENT';
}else{
$tag = 'VALUE';
}
$i = 1;
echo PHP_EOL;
echo "\033[1;36m";
foreach($vals as $val){
if($val['tag'] == $tag){
echo $i++, '> ', $val['value'], PHP_EOL;
}
}
echo "\033[0m";
echo PHP_EOL;
}else{
_usage();
}
function _usage(){
echo <<<USAGE
Usage: ./translate.php [word]
USAGE;
exit(0);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment