Skip to content

Instantly share code, notes, and snippets.

@kunit
Created January 21, 2011 06:47
Show Gist options
  • Save kunit/789333 to your computer and use it in GitHub Desktop.
Save kunit/789333 to your computer and use it in GitHub Desktop.
元が配列である前提で解析して、最後の最後で _raw_xxx というキーを入れる
<?php
class Foo {
function h($text) {
$result = array();
$charset = 'UTF-8';
$this->_escape($text, $result, $charset);
return $result;
}
function _escape($values, &$result, $charset) {
foreach ($values as $key => $value) {
if (is_object($value)) {
$result[$key] = $value;
} else if (is_array($value)) {
$result[$key] = array();
$this->_escape($value, $result[$key], $charset);
} else {
$result[$key] = htmlspecialchars($value, ENT_QUOTES, $charset);
$result["_raw_{$key}"] = $value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment