PHP †phpinfoをコマンドラインで実行 †php -r "phpinfo();" xmlファイルのロード †$xml = simplexml_load_file('hoge.xml'); var_dump($xml); ハイフン付きのメンバ変数名を参照 †
配列の数を取得 †<?php $array = array( "array1", "array2", "array3", "array4" ); echo count( $array ); // 4 と表示される ?> MySQL DB接続、切断 †<?php // 接続用設定 $DB_HOST = "localhost"; $DB_USER = "nuzou"; $DB_PASSWORD = "nuzoudb"; $DB_NAME = "nuzou_pw"; // 接続 function db_connect() { global $DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME; $link = mysqli_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME); return $link; } //切断 function db_close($link) { mysqli_close($link); } ?> 文字コード変換 †UTF8→SJIS function utf8_to_sjis($utf8_string) { return mb_convert_encoding($utf8_string,"SJIS","UTF-8"); } SJIS→UTF8 function sjis_to_utf8($sjis_string) { return mb_convert_encoding($sjis_string,"UTF-8","SJIS"); } 日付計算 †// 現在日付から○日の取得 echo "1日前" . date("Y/m/d", strtotime("-1 day" )); echo "1ヶ月前" . date("Y/m/d", strtotime("-1 month")); echo "1年前" . date("Y/m/d", strtotime("-1 year" )); echo "1週間前" . date("Y/m/d", strtotime("-1 week" )); // 指定日付から○日の取得 echo "1日前" . date("Y/m/d", strtotime("-1 day" ,strtotime("2008/12/01"))); echo "1ヶ月前" . date("Y/m/d", strtotime("-1 month" ,strtotime("2008/12/01"))); echo "1年前" . date("Y/m/d", strtotime("-1 year" ,strtotime("2008/12/01"))); echo "1週間前" . date("Y/m/d", strtotime("-1 week" ,strtotime("2008/12/01"))); // 当月末日 echo date("Y/m/t"); // 前月末日 echo date("Y/m/d", mktime(0,0,0,date("m"),0,date("Y")) ); ファイルアップロード †
プロキシ経由でHTTPリクエスト †$proxy = array( 'http' => array( 'proxy' => 'tcp://<プロキシのアドレス>:<プロキシのポート番号>', 'request_fulluri' => true, ), ); $proxy_context = stream_context_create($proxy); $raw = file_get_contents("<アクセスするURL>", false, $proxy_context); var_dump($raw); Smarty †PDOでSQL Serverに接続 †$pdo = new PDO("odbc:DRIVER={SQL Server};UID=<USER_ID>;PWD=<PASSWORD>;DATABASE=<DB_NAME>;SERVER=<SERVER_NAME>\\<INSTANCE_NAME>" , "<USER_ID>", "<PASSWORD>"); ファイル有無確認 †$filename = 'hogehoge.jpg'; if (file_exists($filename)) { print "$filename ファイルがありました。"; } else { print "$filename ファイルがありません。"; } ファイル読み込み †$filedata = file_get_contents('hogehoge.txt'); ファイル書き込み †$fp = fopen("test.txt", "w"); // w:上書き a:追加 fwrite($fp,"hogehoge"); fclose($fp); CSVファイル読み込み †$filedata = file_get_contents('hogehoge.txt'); $file_line = split("\n", $filedata ); // 行単位に分割 foreach($file_line as $line) { list($data1,$data2,$data3) = split("\t", $line); // TABで分割 echo "1:" . $data1 . " / 2:" . $data2 . " / 3:" . $data3 ."\n"; } foreach †foreach ($array as $value) { echo $value; } function †
MIBのエラーが出たら †
配列の配列の重複削除 †$data = array( array("id" => "1", "data" => "0"), array("id" => "2", "data" => "200"), array("id" => "2", "data" => "0"), array("id" => "2", "data" => "200") ); $tmp = array(); foreach($data as $key => $val){ if(!in_array($val,$tmp)){ $tmp[] = $val; } } $data = $tmp; CentOS 5.6以降でPHP5.3 †
IIS8+PHP5 † |