Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Last active June 24, 2021 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuya-takeyama/402780 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/402780 to your computer and use it in GitHub Desktop.
<?php
/**
* addslashes() for multi-byte characters
*
* @param string $input Input characters
* @param string $enc Internal encoding (optional)
* @return string Charcters added slashes
*/
function mb_addslashes($input, $enc = NULL)
{
if (is_null($enc)) {
$enc = mb_internal_encoding();
}
$len = mb_strlen($input, $enc);
$result = '';
for ($i = 0; $i < $len; $i++)
{
$char = mb_substr($input, $i, 1, $enc);
if (strlen($char) === 1) {
$char = addslashes($char);
}
$result .= $char;
}
return $result;
}
<?php
require_once 'PHPUnit/Framework.php';
require_once 'mb_addslashes.php';
define('INTERNAL_ENCODING', 'UTF-8');
mb_language('Japanese');
ini_set('mbstring.script_encoding', INTERNAL_ENCODING);
ini_set('mbstring.internal_encoding', INTERNAL_ENCODING);
class MbAddslashesTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->input = "これは \"mb_addslashes()\" の'テスト'です。\0\\表能ソ。\\";
$this->expected = "これは \\\"mb_addslashes()\\\" の\\'テスト\\'です。\\0\\\\表能ソ。\\\\";
$this->encodings = array('UTF-8', 'EUC-JP', 'SJIS', 'SJIS-win');
}
public function testAll()
{
foreach ($this->encodings as $enc)
{
$this->_test($enc);
}
}
private function _test($enc)
{
$input = mb_convert_encoding($this->input, $enc, INTERNAL_ENCODING);
$expected = mb_convert_encoding($this->expected, $enc, INTERNAL_ENCODING);
$output = mb_addslashes($input);
$this->assertEquals($output, $this->expected, "The output characters should match with the expected one on '{$enc}'.");
}
}
<?php
require_once 'PHPUnit/Framework.php';
require_once 'mb_addslashes.php';
define('INTERNAL_ENCODING', 'SJIS');
mb_language('Japanese');
ini_set('mbstring.script_encoding', INTERNAL_ENCODING);
ini_set('mbstring.internal_encoding', INTERNAL_ENCODING);
class MbAddslashesTestSjis extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$bs = pack("H*", '5c');
$apos = pack("H*", '27');
$quot = pack("H*", '22');
$null = pack("H*", '00');
$hyo = pack("H*", '955c');
$so = pack("H*", '835c');
$nou = pack("H*", '945c');
$this->input = array(
$bs,
$apos,
$quot,
$null,
$hyo,
$so,
$nou
);
$this->expected = array(
"\\{$bs}",
"\\{$apos}",
"\\{$quot}",
pack("H*", '5c30'),
$hyo,
$so,
$nou
);
$this->input[] = join('', $this->input);
$this->expected[] = join('', $this->expected);
$this->encodings = array('UTF-8', 'EUC-JP', 'SJIS', 'SJIS-win');
}
public function testAll()
{
foreach ($this->encodings as $enc)
{
foreach ($this->input as $i => $in)
{
$this->_test($in, $this->expected[$i], $enc);
}
}
}
private function _test($in, $exp, $enc)
{
$input = mb_convert_encoding($in, $enc, INTERNAL_ENCODING);
$expected = mb_convert_encoding($exp, $enc, INTERNAL_ENCODING);
$output = mb_addslashes($input);
$this->assertEquals($output, $expected, "The output characters should match with the expected one on '{$enc}'.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment