goo blog サービス終了のお知らせ 

かけがえのない日々

なんややんや。にょろにょろ。

ファイルのエンコードが正しくても文字化けする(時々)

2010年10月30日 | PHP
ファイルのエンコードも宣言も正しいのに文字化けする場合があります。
※php.iniの設定かも

そういうときは
ini_set('mbstring.internal_encoding', 'EUC-JP' );
ini_set('mbstring.script_encoding', 'EUC-JP');
ini_set('default_charset', 'EUC-JP');

もしくは
htaccessファイルからの設定変更が許可されているなら
php_value mbstring.internal_encoding EUC-JP
php_value mbstring.script_encoding EUC-JP
php_value default_charset EUC-JP

の3行を追加すればなおった。

スクリプト名を取得する

2010年10月16日 | PHP
//--このファイルのあるディレクトリ名
$require_php_dir = realpath(dirname( __FILE__));

realpath ― 絶対パス名を返す
basename ― パス中のファイル名の部分を返す

//ディレクトリパス
echo dirname(__FILE__)

//スクリプト名
echo basename(__FILE__)

//指定した拡張子を取り除いたスクリプト名
echo basename(__FILE__, '.php')

特殊文字を HTML エンティティに変換する

2010年05月19日 | PHP
'&' (アンパサンド) は '&' になります。
ENT_NOQUOTES が設定されていない場合、 '"' (ダブルクォート) は '"'になります。
ENT_QUOTES が設定されている場合のみ、 ''' (シングルクオート) は '''になります。
'<' (小なり) は '&lt;' になります。
'>' (大なり) は '&gt;' になります。

変数名に変数

2009年02月22日 | PHP
変数名を動的に指定する場合は、

$a = 'hello';
$$a = 'world'; (または $hello = 'world';)
としたとき、以下のような出力。

echo "$a"; → hello
echo "${$a}"; → world
echo "$hello"; → world
変数名の一部だけを可変にする場合は、

$b = 'llo';
echo "$he{$b}"; → エラーとなる!
echo "${'he' . $b}"; → world

画像処理

2008年02月04日 | PHP
//サイズ変更後の画像データを生成
//imagecreate()はgif,png用※255色以上の色は使用できず近似値補完されます
$afr_image1 = imagecreatetruecolor($afr_image_x1, $afr_image_y1);
$afr_image2 = imagecreatetruecolor($afr_image_x2, $afr_image_y2);
$afr_image3 = imagecreatetruecolor($afr_image_x3, $afr_image_y3);
$afr_image4 = imagecreatetruecolor($afr_image_x3, $afr_image_y3);
//イメージのサイズ変更
//imagecopyresized()だと鮮明な画像が作成されます。縮小の場合はimagecopyresampled()が適すると思われます。※ピクセル値を滑らかに補完します
imagecopyresampled($afr_image1, $image, 0, 0, 0, 0, $afr_image_x1, $afr_image_y1, $image_x, $image_y);
imagecopyresampled($afr_image2, $image, 0, 0, 0, 0, $afr_image_x2, $afr_image_y2, $image_x, $image_y);
imagecopyresampled($afr_image3, $image, 0, 0, 0, 0, $afr_image_x3, $afr_image_y3, $image_x, $image_y);
imagecopyresized($afr_image4, $image, 0, 0, 0, 0, $afr_image_x3, $afr_image_y3, $image_x, $image_y);

セッション埋め込み正規表現

2007年12月24日 | PHP
$viewVal = preg_replace( "/(href=\"[^>\?\":]*)\?([^>\?\"]*\"[^>\?\"]*>)/i", "$1?$si&$2", $viewVal );
$viewVal = preg_replace( "/(href=\"[^>\?\":]*)(\"[^>\?\"]*>)/i", "$1?$si$2", $viewVal );
$viewVal = preg_replace( "/<form[^>]*>/i", "$0<input type=\"hidden\" name=si value=\"$si\">", $viewVal );

ファイルリストを取得してリネーム

2006年11月08日 | PHP
$dirName = "../item/".$ary[i_id]."/";

if (file_exists($dirName)) {
//--ディレクトリオープン
if ($handle = opendir($dirName)) {
//--配列リセット
reset($fileAry);
//--ファイル名リストを取得
while (false !== ($file = readdir($handle))) {
if (ereg(".jpg$",$file)) {
$fileAry[] = $file;
}
}
//$fileAry = scandir($dirName);

foreach($fileAry AS $key => $oldFileName){
if (ereg("-",$oldFileName)){
//--新ファイル
$newFileName = ereg_replace($ary[i_id]."-","",$oldFileName);
$renVal = rename($dirName."/".$oldFileName , $dirName."/".$newFileName);
//chmod ($dirName."/".$newFileName , 0666);
if (!$renVal){
$viewVal .= "失n";
}else{
$viewVal .= "成功\n";
}
}
}
closedir($handle);
}
}else{
$mkdir = mkdir ($dirName, 0777);
}