しまったー
初めからこれを使えば良かったー
CodeIgniterを利用したプロジェクトで開発終えてから
CodeIgniterのユーザガイド見てたら思わず上記のように思ってしまったメソッドがあったので ご紹介+備忘録残し で書いときます。
safe_mailto というメソッドなんですが・・・
参考:
URL ヘルパー : CodeIgniter ユーザガイド 日本語版.
まず、いつものようにヘルパーの呼び出し
$this->load->helper('url');
で、Viewとなるphpに
<?php echo safe_mailto('hogehoge@hoge.hoge'); ?>
って書いちゃうだけで
この「 hogehoge@hoge.hoge 」というメールアドレスをJavaScriptで難読化して表示してくれちゃう。
あー、これ使えば良かった。
独自におんなじようなことやってた・・・
ちなみにコードは下の通り。
function safe_mailto($email, $title = '', $attributes = '')
{
$title = (string) $title;
if ($title == "")
{
$title = $email;
}
for ($i = 0; $i < 16; $i++)
{
$x[] = substr('<a href="mailto:', $i, 1);
}
for ($i = 0; $i < strlen($email); $i++)
{
$x[] = "|".ord(substr($email, $i, 1));
}
$x[] = '"';
if ($attributes != '')
{
if (is_array($attributes))
{
foreach ($attributes as $key => $val)
{
$x[] = ' '.$key.'="';
for ($i = 0; $i < strlen($val); $i++)
{
$x[] = "|".ord(substr($val, $i, 1));
}
$x[] = '"';
}
}
else
{
for ($i = 0; $i < strlen($attributes); $i++)
{
$x[] = substr($attributes, $i, 1);
}
}
}
$x[] = '>';
$temp = array();
for ($i = 0; $i < strlen($title); $i++)
{
$ordinal = ord($title[$i]);
if ($ordinal < 128)
{
$x[] = "|".$ordinal;
}
else
{
if (count($temp) == 0)
{
$count = ($ordinal < 224) ? 2 : 3;
}
$temp[] = $ordinal;
if (count($temp) == $count)
{
$number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
$x[] = "|".$number;
$count = 1;
$temp = array();
}
}
}
$x[] = '<'; $x[] = '/'; $x[] = 'a'; $x[] = '>';
$x = array_reverse($x);
ob_start();
?><script type="text/javascript">
//<![CDATA[
var l=new Array();
<?php
$i = 0;
foreach ($x as $val){ ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?>
for (var i = l.length-1; i >= 0; i=i-1){
if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
else document.write(unescape(l[i]));}
//]]>
</script><?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
あと、同ユーザガイド内の「auto_link」が好きです。
もともと、auto_link を他でも使いたいと思ってソース見たら、その上にあったメソッドがこれだった・・・というわけ。
次にCodeIgniter使うプロジェクトは新バージョンだろうな~。
