[PHP-users 1610] Re: addslashes関数でエンコード

php-users@php.gr.jp php-users@php.gr.jp
Tue, 28 Aug 2001 19:42:30 +0900


たけです。
佐川さん、コメントありがとうございます。

http://ns1.php.gr.jp/php-jp/archives/msg08777.htmlより

function addslashes_ext2($sjis) {
  $sjis_high = "[\x81-\x9f\xe0-\xfc]";
  $sjis_low  = "[\x40-\x7e\x80-\xfc]";
  $ank       = "[\x01-\x7f\xa0-\xdf]";
  $escaped = addslashes($sjis);
  if (ereg("($sjis_high\\\\)\\\\", $escaped)) {
     // SLOW replace mode
     // XXX: ereg_replace may not support \x00. So, use \xff as delimiter.
     //      (preg_replace supports \x00)
     $escaped = ereg_replace("(${sjis_high}${sjis_low}|$ank)", "\\1\xff",
                $escaped);
     $escaped = ereg_replace("(\xff$sjis_high\\\\\xff)\\\\\xff", "\\1",
                $escaped);
     $escaped = str_replace("\xff", "", $escaped);
  }
  return $escaped;
}

addslashes関数をaddslashes_ext2にかえればよいみたいなのですが、
> (3) 自作のaddslashesを作成する
これにあたるわけですね。(自作ではないですがf^^;)

そこでaddslashes_ext2の関数をいろいろと利用してみたのですが、

$str="本能";
print $str;
//	出力結果
//	Parse error: parse error in ・・・・

$str="本能";
$str = addslashes_ext2($str);
print $str;
//	出力結果
//	本能

$str="能";
$str = addslashes_ext2($str);
print $str;
//	出力結果
//	能\

となりました。

「能」の場合では「能\」となってしまうので、
$query="INSERT INTO  test1 (str) VALUES('能\')";
$result = mysql_query($query);
となってしまって
1064: You have an error in your SQL syntax near ''能\')' at line 1
となってしまいます。

これはaddslashes_ext2側の問題となるのでしょうか?

よろしくお願いします。



J-SKY研究所
http://www.j-ken.ne.jp
j-ken/take <take@j-ken.com>