_onichannn

【PHP】指定したディレクトリ下のファイル、フォルダを再帰的に削除する関数。

メモ。

下記関数に削除したいディレクトリを投げればOK。
投げたディレクトリが相対パス、またはシンボリックリンクの場合、第二引数にtrueを渡すと参照を解決してくれる。

function destroyDir($dir, $virtual = false) {  
    $ds = DIRECTORY_SEPARATOR;  
    $dir = $virtual ? realpath($dir) : $dir;  
    $dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;  
    if(is_dir($dir) && $handle = opendir($dir)) {  
        while($file = readdir($handle)) {  
            if($file == '.' || $file == '..') {  
                continue;  
            } elseif(is_dir($dir.$ds.$file)) {  
                destroyDir($dir.$ds.$file);  
            } else {  
                unlink($dir.$ds.$file);  
            }  
        }  
        closedir($handle);  
        rmdir($dir);  
        return true;  
    } else {  
        return false;  
    }  
}

 

【PHP】メールアドレスとテキストからmailtoのリンクを生成する。

ユニークな関数を見つけたのでメモ。

function encode_email($email, $linkText, $attrs = '')  
{  
    // remplazar aroba y puntos  
    $email = str_replace('@', '@', $email);  
    $email = str_replace('.', '.', $email);  
  
    $linkText = str_replace('@', '@', $linkText);  
    $linkText = str_replace('.', '.', $linkText);  
      
    $part1 = '<a href="ma';  
    $part2 = 'ilto&#58;';  
    $part3 = '" '. $attrs .' >';  
    $part4 = '</a>';  
  
    $encoded = '<script type="text/javascript">';  
    $encoded .= "document.write('$part1');";  
    $encoded .= "document.write('$part2');";  
    $encoded .= "document.write('$email');";
    $encoded .= "document.write('$part3');";  
    $encoded .= "document.write('$linkText');";
    $encoded .= "document.write('$part4');";  
    $encoded .= '</script>';  
  
    return $encoded;  
}  

下記のように使う。

$email = 'test@example.com';
$link = 'メールはこちら。';
$attrs = 'class = "test"';
echo encode_email($email, $link, $attrs);

すると下記HTMLが生成される。

<script type="text/javascript">document.write('<a href="ma');document.write('ilto&#58;');document.write('test&#64;example&#46;com');document.write('" class = "test" >');document.write('メールはこちら。');document.write('</a>');</script>

結果下記コードがブラウザに出力される。

<a href="mailto:test@example.com" class="test">メールはこちら。</a>

面白いね。

 

【PHP】文字列を圧縮する。

膨大な量の文字列をやりとりする場合の選択肢として一つめも。

$string ='aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee';
 
$compressed = gzcompress($string);
 
echo "Original size: ".strlen($string)."\n";

echo "Compressed size: ".strlen($compressed)."\n";

↓

Original size: 50
Compressed size: 21

文字列を展開したい場合は下記の通り。

$original = gzuncompress($compressed);

 

【PHP】マジック定数まとめ。

自分用。

__LINE__ 現在の行数を返す
__FILE__ 現在のファイルパスを返す
__DIR__ 現在のディレクトリパスを返す
__FUNCTION__ 現在処理中の関数名を返す
__CLASS__ 現在処理中のクラス名を返す
__METHOD__ クラスのメソッド名を返す
__NAMESPACE__ 現在の名前空間名を返す

 

【PHP】ソースコードををハイライトして出力する。

こんな素敵な関数がデフォルトで存在していたなんて。

highlight_string('<?php phpinfo(); ?>');

この出力結果は下記となる。

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;phpinfo</span><span style="color: #007700">();&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code>

」というコードのシンタックスをハイライトしてくれている。

非常に素敵。

 

【PHP】数値に順位をつけて返却してくれるラッパー関数。

面白いやつを見つけたのでメモ。
1st、2nd、3rdといった感じ。

function ordinal($cdnl){
	$test_c = abs($cdnl) % 10;
	$ext = ((abs($cdnl) % 100 < 21 && abs($cdnl) % 100 > 4) ? 'th'
			: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
					? 'th' : 'st' : 'nd' : 'rd' : 'th'));
	return $cdnl.$ext;
}

for($i = 1; $i < 40; $i++){
	echo ordinal($i).'<br>';
}

↓

1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th
30th
31st
32nd
33rd
34th
35th
36th
37th
38th
39th

これは便利。

 

【Excel】半角スペースで2分割した文字の左右をそれぞれ取得する。

めも。

■2分割した左側の文字列を取得する。

=LEFT(A1,FIND(" ",SUBSTITUTE(A1," "," "))-1)

■2分割した右側の文字列を取得する。

=RIGHT(A1,LEN(A1)-FIND(" ",SUBSTITUTE(A1," "," ")))

 

【Javascript】ページのコンテンツを下からふわふわインサートさせる。

FlowUp.jsを使ってみる。

http://www.dominikgorecki.com/download/flow-up/

画面を下にスクロールすると、どんどん新しい要素が追加されていく。
ajaxのページャーみたいな挙動だけど、ちょっと独特で面白い。