超素人のJAVAスクリプト&PHP奮戦記

超ど素人のえいすけがJAVAスクリプトとPHPを勉強してみるブログ

さいころプログラムの作り方

2011-01-05 | PHP
他のプログラム言語だと結構めんどくさい「さいころプログラム」。
1から6までの数値をランダムに表示します。
PHPなら一行で完成してしまいます。
ソースは以下のようなモノです。

<?php echo mt_rand(1,6);?>
※サニタイジング対策のため一部全角文字にしています

恐ろしいほど簡単です^^;;;;
学生時代、c言語を挫折した私でも何となく使えてしまう、それがPHP。
一生使いますw

phpで天気予報を表示させるプログラムを作ってみた。ブログパーツにも使えます。

2010-10-27 | PHP
残念ながらPHPの文章はそのまま掲載されないみたいです。
実行可能な形式にするには、修正を加えなきゃいけないみたいです。
んー、やっぱり画像で掲載するしかないのかなぁ

/* XMLファイルの内容を取得 */
$tenki = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf'); // ブログタイトル取得
echo $tenki->channel->title."\n";
// ブログ説明取得
echo $tenki->channel->description."\n";
for($i = 0; $i < 5; $i++) {
// 記事のタイトル取得
$title = $tenki->channel->item[$i]->title;}

// HTML整形
echo $title;
?>




/* XMLファイルの内容を取得 */
$rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
foreach ($rss->channel->item as $title){
$title = $rss->channel->item->title;
if(ereg("村山", $title)){
echo $rss->channel->item->title."\n";
break;
}
}
?>







/* RDFファイルを取得 */
$rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
/* RDFに書かれている特定の地方を記述 */
$area = "村山";
/* RDFに書かれている天候を記述 */
global $weather = "晴|曇|雨|雪|霧";
/* 初期化 */
$value=0;

foreach ($rss->channel->item as $title){
$title = $rss->channel->item->title;
if(ereg("$area", $title)){
$strlen = mb_strlen($title);
while ($strlen) {
$array[] = mb_substr($title,0,1,"UTF-8");
$title = mb_substr($title,1,$strlen,"UTF-8");
$strlen = mb_strlen($title);
}
break;
}
}

for($i=0; $i<count($array); $i++ ){
if(ereg("$weather",$array[$i])){
$dayforecast[$value]=$array[$i];
$value++;
}
}

echo "今日午前".$dayforecast[0]."<hr />";
echo "今日午後".$dayforecast[1]."
";
echo "明日午前".$dayforecast[2]."
";
echo "明日午後".$dayforecast[3]."
";



?>

phpで天気予報を表示させるプログラムを作ってみた。ブログパーツにも使えます。

2010-10-27 | PHP
/* XMLファイルの内容を取得 */
$tenki = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf'); // ブログタイトル取得
echo $tenki->channel->title."
\n";
// ブログ説明取得
echo $tenki->channel->description."
\n";
for($i = 0; $i < 5; $i++) {
// 記事のタイトル取得
$title = $tenki->channel->item[$i]->title;}

// HTML整形
echo $title;
?>




/* XMLファイルの内容を取得 */
$rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
foreach ($rss->channel->item as $title){
$title = $rss->channel->item->title;
if(ereg("村山", $title)){
echo $rss->channel->item->title."
\n";
break;
}
}
?>







/* RDFファイルを取得 */
$rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
/* RDFに書かれている特定の地方を記述 */
$area = "村山";
/* RDFに書かれている天候を記述 */
global $weather = "晴|曇|雨|雪|霧";
/* 初期化 */
$value=0;

foreach ($rss->channel->item as $title){
$title = $rss->channel->item->title;
if(ereg("$area", $title)){
$strlen = mb_strlen($title);
while ($strlen) {
$array[] = mb_substr($title,0,1,"UTF-8");
$title = mb_substr($title,1,$strlen,"UTF-8");
$strlen = mb_strlen($title);
}
break;
}
}

for($i=0; $i<count($array); $i++ ){
if(ereg("$weather",$array[$i])){
$dayforecast[$value]=$array[$i];
$value++;
}
}

echo "今日午前".$dayforecast[0]."<hr />";
echo "今日午後".$dayforecast[1]."
";
echo "明日午前".$dayforecast[2]."
";
echo "明日午後".$dayforecast[3]."
";



?>



















/* RDFファイルを取得 */
$rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
/* RDFに書かれている特定の地方を記述 */
$area = "村山";
/* RDFに書かれている天候を記述 */
$weather = "晴|曇|雨|雪|霧|雷";
/* 掲載するホームページ */
$url = "http://localhost/jidaiya/";
/* 初期化 */
$value=0;$time=0;

foreach ($rss->channel->item as $title){
$title = $rss->channel->item->title;
if(ereg("$area", $title)){
$strlen = mb_strlen($title);
while ($strlen) {
$array[] = mb_substr($title,0,1,"UTF-8");
$title = mb_substr($title,1,$strlen,"UTF-8");
$strlen = mb_strlen($title);
}
break;
}
}

for($i=0; $i<count($array); $i++ ){
if(ereg("$weather",$array[$i])){
$dayforecast[$value]=$array[$i];
$value++;
}
}


echo timecheak($array).imgchange($dayforecast[0],$url)."<hr />";
echo timecheak($array).imgchange($dayforecast[1],$url)."
";
echo "明日午前".imgchange($dayforecast[2],$url)."
";
echo "明日午後".imgchange($dayforecast[3],$url)."
";
echo "明後日午前".imgchange($dayforecast[4],$url)."
";
echo "明後日午後".imgchange($dayforecast[5],$url)."
";


function timecheak($arr){
for($i=0; $i<count($arr); $i++ ){
if(ereg("夜",$arr[$i])){
return $time="今夜";
break;
}
}
return $time="今日";
}

function imgchange($text,$url){
switch ($text){
case '晴':return $wetherimage=$url."/wether-img/1.gif";break;
case '曇':return $wetherimage=$url."/wether-img/2.gif";break;
case '雨':return $wetherimage=$url."/wether-img/3.gif";break;
case '雪':return $wetherimage=$url."/wether-img/4.gif";break;
case '霧':return $wetherimage=$url."/wether-img/5.gif";break;
case '雷':return $wetherimage=$url."/wether-img/7.gif";break;
default:$wetherimage='未登録の天候です';break;
}
}

?>












/* RDFファイルを取得 */
$rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
/* RDFに書かれている特定の地方を記述 */
$area = "村山";
/* RDFに書かれている天候を記述 */
$weather = "晴|曇|雨|雪|霧|雷";
/* 掲載するホームページ */
$url = "http://localhost/jidaiya/";

//基本的な変更終了!↓触らなくてもOK!

/* 初期化 */
$value=0;$time=0;

foreach ($rss->channel->item as $title){
$title = $rss->channel->item->title;
if(ereg("$area", $title)){
$strlen = mb_strlen($title);
while ($strlen) {
$array[] = mb_substr($title,0,1,"UTF-8");
$title = mb_substr($title,1,$strlen,"UTF-8");
$strlen = mb_strlen($title);
}
break;
}
}

for($i=0; $i<count($array); $i++ ){
if(ereg("$weather",$array[$i])){
$dayforecast[$value]=$array[$i];
$value++;
}
}

/*このecho関数で情報を吐き出します。CSSの変更などを加えたいときはここを触るといいかも?
 自分もよくわかっていませんが、連絡いただければご一緒に悩んでみます:-)*/

echo "<ul>\n
  • ".timecheak($array).imgchange($dayforecast[0],$url)." / ".imgchange($dayforecast[1],$url)."
  • \n";
    echo "
  • 明日".imgchange($dayforecast[2],$url)." / ".imgchange($dayforecast[3],$url)."
  • \n";
    echo "
  • 明後日".imgchange($dayforecast[4],$url)." / ".imgchange($dayforecast[5],$url)."
  • \n\n";


    function timecheak($arr){
    for($i=0; $i<count($arr); $i++ ){
    if(ereg("夜",$arr[$i])){
    return $time="今夜";
    break;
    }
    }
    return $time="今日";
    }

    function imgchange($text,$url){
    switch ($text){
    case '晴':return $wetherimage="<img src=\"".$url."/wether-img/1.gif\" alt=\"晴れ\"/>";break;
    case '曇':return $wetherimage="\"";break;
    case '雨':return $wetherimage="\"";break;
    case '雪':return $wetherimage="\"";break;
    case '霧':return $wetherimage="\"";break;
    case '雷':return $wetherimage="\"";break;
    default:$wetherimage='未登録の天候です';break;
    }
    }

    ?>































    /* RDFファイルを取得 */
    $rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
    /* RDFに書かれている特定の地方を記述 */
    $area = "村山";
    /* RDFに書かれている天候を記述 */
    $weather = "晴|曇|止|雨|雪|霧|雷";
    /* 掲載するホームページ */
    $url = "http://localhost/jidaiya/";

    //基本的な変更終了!↓触らなくてもOK!

    /* 初期化 */
    $value=0;$time=0;

    foreach ($rss->channel->item as $title){
    $title = $rss->channel->item->title;
    if(ereg("$area", $title)){
    $strlen = mb_strlen($title);
    while ($strlen) {
    $array[] = mb_substr($title,0,1,"UTF-8");
    $title = mb_substr($title,1,$strlen,"UTF-8");
    $strlen = mb_strlen($title);
    }
    break;
    }
    }

    for($i=0; $i<count($array); $i++ ){
    if(ereg("$weather",$array[$i])){
    $dayforecast[$value]=$array[$i];
    $value++;
    }
    }

    /*このecho関数で情報を吐き出します。CSSの変更などを加えたいときはここを触るといいかも?
     自分もよくわかっていませんが、連絡いただければご一緒に悩んでみます:-)*/

    echo "<ul>\n
  • ".timecheak($array).imgchange($dayforecast[0],$url)." / ".imgchange($dayforecast[1],$url)."
  • \n";
    echo "
  • 明日".imgchange($dayforecast[2],$url)." / ".imgchange($dayforecast[3],$url)."
  • \n";
    echo "
  • 明後日".imgchange($dayforecast[4],$url)." / ".imgchange($dayforecast[5],$url)."
  • \n\n";


    function timecheak($arr){
    for($i=0; $i<count($arr); $i++ ){
    if(ereg("夜",$arr[$i])){
    return $time="今夜";
    break;
    }
    }
    return $time="今日";
    }

    function imgchange($text,$url){
    switch ($text){
    case '晴':return $wetherimage="<img src=\"".$url."/wether-img/1.gif\" alt=\"晴れ\"/>";break;
    case '曇':return $wetherimage="\"";break;
    case '止':return $wetherimage="\"";break;
    case '雨':return $wetherimage="\"";break;
    case '雪':return $wetherimage="\"";break;
    case '霧':return $wetherimage="\"";break;
    case '雷':return $wetherimage="\"";break;
    default:$wetherimage='未登録の天候です';break;
    }
    }

    ?>






















    /* RDFファイルを取得 */
    $rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
    /* RDFに書かれている特定の地方を記述 */
    $area = "村山";
    /* RDFに書かれている天候を記述 */
    $weather = "晴|曇|止|雨|雪|霧|雷";
    /* 掲載するホームページ */
    $url = "http://localhost/jidaiya/";

    //基本的な変更終了!↓触らなくてもOK!

    /* 初期化 */
    $value=0;$time=0;$DAT=0;

    foreach ($rss->channel->item as $title){
    $title = $rss->channel->item->title;
    if(ereg("$area", $title)){
    if(ereg("〔.+〕", $title)){

    }else{
    if(!ereg("明後日", $title)){$DAT=1;}
    $strlen = mb_strlen($title);
    while ($strlen) {
    $array[] = mb_substr($title,0,1,"UTF-8");
    $title = mb_substr($title,1,$strlen,"UTF-8");
    $strlen = mb_strlen($title);
    }
    break;
    }
    }

    for($i=0; $i<count($array); $i++ ){
    if(ereg("$weather",$array[$i])){
    $dayforecast[$value]=$array[$i];
    $value++;
    }
    }

    /*このecho関数で情報を吐き出します。CSSの変更などを加えたいときはここを触るといいかも?
     自分もよくわかっていませんが、連絡いただければご一緒に悩んでみます:-)*/

    echo "<ul>\n
  • ".timecheak($array).imgchange($dayforecast[0],$url)." / ".imgchange($dayforecast[1],$url)."
  • \n";
    echo "
  • 明日".imgchange($dayforecast[2],$url)." / ".imgchange($dayforecast[3],$url)."
  • \n";
    if($DAT=0){echo "
  • 明後日".imgchange($dayforecast[4],$url)." / ".imgchange($dayforecast[5],$url)."
  • \n\n";}


    function timecheak($arr){
    for($i=0; $i<count($arr); $i++ ){
    if(ereg("夜",$arr[$i])){
    return $time="今夜";
    break;
    }
    }
    return $time="今日";
    }

    function imgchange($text,$url){
    switch ($text){
    case '晴':return $wetherimage="<img src=\"".$url."/wether-img/1.gif\" alt=\"晴れ\"/>";break;
    case '曇':return $wetherimage="\"";break;
    case '止':return $wetherimage="\"";break;
    case '雨':return $wetherimage="\"";break;
    case '雪':return $wetherimage="\"";break;
    case '霧':return $wetherimage="\"";break;
    case '雷':return $wetherimage="\"";break;
    default:$wetherimage='未登録の天候です';break;
    }
    }

    ?>













    /* RDFファイルを取得 */
    $rss = simplexml_load_file('http://tenki.wet.co.jp/forecast/day01ymg.rdf');
    /* RDFに書かれている特定の地方を記述 */
    $area = "村山";
    /* 掲載するホームページ */
    $url = "http://localhost/jidaiya/";

    //基本的な変更終了!↓触らなくてもOK!

    /* RDFに書かれている天候を記述 */
    $weather = "晴|曇|止|雨|雪|霧|雷";
    /* 初期化 */
    $AT=0;$DAT=0;$out=0;$loop=0;$analyze=0;

    foreach ($rss->channel->item as $title){
    $title = $rss->channel->item->title;
    if(ereg("$area", $title)){
    if(ereg("明日", $title)){$AT=1;}
    if(ereg("明後日", $title)){$DAT=1;}
    preg_match_all("|〔(.*)〕|U","$title",$out, PREG_PATTERN_ORDER);
    break;
    }
    }

    for($i=0;$i<3;$i++){
    $analyze=lyze($out[1][$i],$weather,$i);
    echo $out[1][i]."<br />";
    for($j=0;$j<2;$j++){
    $wetherinfo[$loop]=$analyze[$j];
    $loop++;
    }
    }

    /*このecho関数でHTMLを吐き出します。CSSの変更などを加えたいときはここを触るといいかも?
     自分もよくわかっていませんが、連絡いただければご一緒に悩んでみます:-)*/

    ?>
    • ".timecheak($title).imgchange($wetherinfo[0],$url)." / ".imgchange($wetherinfo[1],$url)."
    • \n";
      if($AT=1){echo "
    • 明日".imgchange($wetherinfo[2],$url)." / ".imgchange($wetherinfo[3],$url)."
    • \n";}
      if($DAT=1){echo "
    • 明後日".imgchange($wetherinfo[4],$url)." / ".imgchange($wetherinfo[5],$url)."
    • \n";}
      ?>
    <?php


    function lyze($text,$weather,$value){
    $flag=0;
    $strlen = mb_strlen($text);
    while ($strlen) {
    $array[] = mb_substr($text,0,1,"UTF-8");
    $text = mb_substr($text,1,$strlen,"UTF-8");
    $strlen = mb_strlen($text);
    }



    if(count($array)==1){
    if($value==0){$dayforecast[0]=$array[0];$dayforecast[1]=$array[0];return $dayforecast;}
    if($value==1){$dayforecast[2]=$array[0];$dayforecast[3]=$array[0];return $dayforecast;}
    if($value==2){$dayforecast[4]=$array[0];$dayforecast[5]=$array[0];return $dayforecast;}
    }else{
    for($i=0; $i<count($array); $i++ ){
    if(ereg("$weather",$array[$i])){
    $dayforecast[$flag]=$array[$i];
    $flag++;
    }
    }
    return $dayforecast;
    }
    }

    function timecheak($text){
    if(ereg("今夜",$text)){
    return $time = "今夜" ;
    }else{
    return $time = "今日" ;
    }
    }

    function imgchange($text,$url){
    switch ($text){
    case '晴':return $wetherimage="<img src=\"".$url."/wether-img/1.gif\" alt=\"晴れ\"/>";break;
    case '曇':return $wetherimage="\"";break;
    case '止':return $wetherimage="\"";break;
    case '雨':return $wetherimage="\"";break;
    case '雪':return $wetherimage="\"";break;
    case '霧':return $wetherimage="\"";break;
    case '雷':return $wetherimage="\"";break;
    default:$wetherimage='未登録の天候です';break;
    }
    }

    ?>

    配列の簡単な出力の仕方

    2009-05-14 | PHP
    たまーに配列の中に何を入れていたかわからなくなりますよね。

    バグがあったときなんかは特に配列の中を調べたくなります。

    ちょっと前までは、foreach構文を使ってはき出させていたのですが少々めんどくさい。

    そこで良い命令を発見したのでメモメモ







    print_r命令を使うと視覚的に見やすい形ではき出してくれるみたいです。

    おすすめですよ。



    二次元配列の連想配列

    2009-05-12 | PHP
    二次元配列シリーズの記事が増えつつありますね

    今日は、連想配列で二次元配列を作ります。

    連想配列ってのは $data[man]みたいに文字列に情報を保存する配列でした。

    $data[0] みたいに番号だとわかりにくい場合がありますよね。









    前回と違う点は、連想配列を使っているところですよ。

    データをいているところに名前が付きました。

    データの保存状態を考えると、下のようになります。



       name          sex       age

     0 yamada         man        20

     1 shimada         woman      23

     2 yamaguchi        man       45

     3 suzuki           woman     85



    ちなみに、連想配列の箱の名前(キー)は日本語でも大丈夫みたいです。

    ただし文字のエンコードをutf-8にする必要があるようですが。