I know this is probably a really simple thing, but i am just getting started with php, any help would be appreciated!!!
I am trying to adapt a script that gets the weather from an rss feed and writes it onto a webpage. there are already many scripts that do this, but i need to have it in 2 languages. rather than write the same script twice, it would be nice to just use it once with the variable being the language. right now, the variable is the weather's source url:
processWeather('http://weather.yahooapis.com/forecastrss?p=TWXX0021&u=c');
the script will already do the work of translating, so i don't need more than one weather source, but i don't know how to re-write the function to output based on language. for instance it would be nice to have it work like this:
processWeather('chinese');
here is what i got:
function getWeatherRSS($weatherLink){
if ($fp = fopen($weatherLink, 'r')) {
$content = '';
while ($line = fread($fp, 1024)) {
$content .= $line;
}
}
return $content;
}
function processWeather($wurl){
$wrss = getWeatherRSS($wurl);
$unit = '';
$temp = '-';
$city = '';
$code = '';
$char = '';
$cond = '';
if (strlen($wrss)>100) {
// Get unit (C or F)
$spos = strpos($wrss,'yweather:units temperature="')
+strlen('yweather:units temperature="');
$epos = strpos($wrss,'"',$spos);
if ($epos>$spos) {
$unit = substr($wrss,$spos,$epos-$spos);
}
// Get temperature
$spos = strpos($wrss,'yweather:wind chill="')
+strlen('yweather:wind chill="');
$epos = strpos($wrss,'"',$spos);
if ($epos>$spos) {
$temp += substr($wrss,$spos,$epos-$spos);
} else {
$temp = '-';
}
// Get city name
$spos = strpos($wrss,'yweather:location city="')
+strlen('yweather:location city="');
$epos = strpos($wrss,'"',$spos);
if ($epos>$spos) {
$city = substr($wrss,$spos,$epos-$spos);
}
// Get weather code
$spos = strpos($wrss,'" code="')
+ strlen('" code="');
$epos = strpos($wrss,'"',$spos);
if ($epos>$spos) {
$code = substr($wrss,$spos,$epos-$spos);
}
// Convert weather code to chinese characters
function code2char($string) {
$search = array('0' => '龍捲風',
'1' => '熱帶風暴',
'2' => '颶風',
'3' => '強雷暴',
'4' => '雷雨',
'5' => '混合雨和雪',
'6' => '混合雨和雨夾雪',
'7' => '混合雪, 雨夾雪',
'8' => '凍結毛毛雨',
'9' => '小雨',
'10' => '凍雨',
'11' => '雨',
'12' => '雨',
'13' => '大雪小雪',
'14' => '小雪陣',
'15' => '吹雪',
'16' => '雪',
'17' => '冰雹',
'18' => '雨夾雪',
'19' => '塵埃',
'20' => '霧',
'21' => '霾',
'22' => '黑煙',
'23' => '大風',
'24' => '風',
'25' => '冷',
'26' => '多雲',
'27' => '多雲',
'28' => '多雲',
'29' => '晴間多雲',
'30' => '晴間多雲',
'31' => '清除',
'32' => '陽光',
'33' => '公平',
'34' => '公平',
'35' => '混合雨和冰雹',
'36' => '熱',
'37' => '局部地區性雷暴',
'38' => '零星雷暴',
'39' => '零星雷暴',
'40' => '零星陣雨',
'41' => '大雪',
'42' => '零星陣雪',
'43' => '大雪',
'44' => '多雲',
'45' => '雷陣雨',
'46' => '陣雪',
'47' => '孤立雷陣雨');
return strtr($string, $search);
}
$char = code2char($code);
// Get conditions
$spos = strpos($wrss,'yweather:condition text="')
+ strlen('yweather:condition text="');
$epos = strpos($wrss,'"',$spos);
if ($epos>$spos) {
$cond = substr($wrss,$spos,$epos-$spos);
}
}
$toolbar = '台北市: '.$char.' '.$temp.'°'.$unit;
return $toolbar;
}
print(processWeather('http://weather.yahooapis.com/forecastrss?p=TWXX0021&u=c'));
sorry if this is really basic stuff... just not too sure what i'm doing yet, and i can't find similar examples... any helpful explainations would be great.