[PHP]正規表現における「¥」
正規表現における「¥」
「\\」ではなく「\\\\」
$t = preg_split("/\\\\/",__FILE__);
URLを取得
$url = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]
暗号化・複合化
define('KEY', 'bsrNaTwou3PJJbUHMVp20BW0JFxszbOdTaYTtY2jD26TxNFZ');//getRandomStr(48)で取得 define('IV', 'rfKvr+rLztklJ0/6hc8ZmrILlMLVoan6ltu/SR2Bvzg=');//getCryptIV()で取得 $str='あいうえお'; $crypted_str = getEncrypt($str); echo $crypted_str; echo '<br/><br/>'; $decrypted_str = getDecrypt($crypted_str); echo $decrypted_str; exit; function getEncrypt($data) { $resource = mcrypt_module_open('rijndael-256', '', 'ofb', ''); $iv = base64_decode(str_rot13(IV)); $ks = mcrypt_enc_get_key_size($resource); $key = substr(md5(KEY), 0, $ks); mcrypt_generic_init($resource, $key, $iv); $encrypt = mcrypt_generic($resource, $data); mcrypt_generic_deinit($resource); mcrypt_module_close($resource); return base64_encode($encrypt); } function getDecrypt($data) { $resource = mcrypt_module_open('rijndael-256', '', 'ofb', ''); $iv = base64_decode(str_rot13(IV)); $ks = mcrypt_enc_get_key_size($resource); $key = substr(md5(KEY), 0, $ks); mcrypt_generic_init($resource, $key, $iv); $decrypt = mdecrypt_generic($resource, base64_decode($data)); mcrypt_generic_deinit($resource); mcrypt_module_close($resource); return $decrypt; } function getCryptIV(){ $key = getRandomStr(48); $td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $iv = base64_encode($iv); mcrypt_module_close($td); return $iv; } function getRandomStr($length=8){ $str=''; for ($i = 0, $str = null; $i < $length; ) { $num = mt_rand(0x30, 0x7A); if ((0x30 <= $num && $num <= 0x39) || (0x41 <= $num && $num <= 0x5A) || (0x61 <= $num && $num <= 0x7A)) { $str .= chr($num); $i++; } } return $str; }
INSERT文を作る
echo getInsertQueryByKey('system_table', array('name','gender'), array( array('jeff','male'), array('pola','female') ) ); function getInsertQueryByKey($tableName,$columns,$datas){ //SQLite古いバージョン $w=array(); $column = implode(',', $columns); $query="INSERT INTO {$tableName} ({$column}) SELECT "; $tmp=array(); foreach ($columns as $index => $columnName) { $tmp[] = "'". $datas[0][$index] . "'" . ' AS ' . $columnName; } $query .= implode(', ', $tmp); $w[]=$query; foreach ($datas as $index => $data) { if($index<1)continue; $tmp=array(); foreach ($columns as $index2 => $columnName) { $tmp[]=$data[$index2]; } $w[]="'".implode("', '", $tmp)."'"; } return implode(' UNION ALL SELECT ', $w).';'; /* MySQL or SQLite(バージョン 3.7.11以降) $queryValues=array(); foreach ($datas as $index => $value) { $values=array(); foreach ($value as $index2 => $value2) { $values[]="'".$value2."'"; } $queryValues[]='('.implode(',', $values).')'; } $column=implode(',', $columns); $value=implode(',', $queryValues); $sql="INSERT INTO {$tableName} ({$column}) VALUES {$value};"; return $sql; */ }
配列の値だけの配列を作る
$data=array(); $data['jack']=array( 'gender'=>'male', 'size'=>198, 'status'=>array('QK',32,'skyhigh','log'=>array('20120303','20120529','20121111') ) ); $data['jeff']=array( 4,87,8,4,3 ); $data['pole']=array( 'address'=>'earth', 'phone number'=>'39028502753' ); echo '<pre>'; var_dump( getValuesByArray($data) ); echo '</pre>'; function getValuesByArray($targetArray){ if( !is_array($targetArray) ){ return array($targetArray); } $tmp=array(); foreach($targetArray as $key => $value){ $tmp=array_merge($tmp, getValuesByArray($value) ); } return $tmp; }
出力結果
array(15) { [0]=> string(4) "male" [1]=> int(198) [2]=> string(2) "QK" [3]=> int(32) [4]=> string(7) "skyhigh" [5]=> string(8) "20120303" [6]=> string(8) "20120529" [7]=> string(8) "20121111" [8]=> int(4) [9]=> int(87) [10]=> int(8) [11]=> int(4) [12]=> int(3) [13]=> string(5) "earth" [14]=> string(11) "39028502753" }
テーブルのカラム名を取得
$this->Model->getColumnTypes();
Controller
$columnTypes=$this->SystemTest->getColumnTypes(); $columnNames=array_keys($columnTypes); $this->set('columnTypes', $columnTypes); $this->set('columnNames', $columnNames);
View
columnTypes<br/> <?php $tmp=var_export($columnTypes,true); echo str_replace("\n", "<br/>", $tmp); ?> <br/><br/> columnNames<br/> <?php $tmp=var_export($columnNames,true); echo str_replace("\n", "<br/>", $tmp); ?>
MySQLを使う
$default = array( 'host' => 'database host', 'user' => 'user ID', 'password' => 'password', 'database' => 'database name', 'encoding' => 'utf8', ); $link = mysql_connect($default['host'], $default['user'], $default['password']); if (!$link) { echo '接続失敗です。'.mysql_error(); die(); } echo "接続成功<br/><br/>"; $db_selected = mysql_select_db($default['database'], $link); if (!$db_selected){ echo 'データベース選択失敗です。'.mysql_error(); die(); } echo "データベース選択成功<br/><br/>"; mysql_set_charset($default['encoding']); $result = mysql_query('SELECT * FROM test00'); if (!$result) { echo 'クエリーが失敗しました。'.mysql_error(); die(); } $tmp=array(); while ($row = mysql_fetch_assoc($result)) { $tmp[]=$row; } var_dump($tmp); $close_flag = mysql_close($link); echo '切断に'.( ($close_flag)?'成功':'失敗' ).'しました。';