mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Add array_escape() function to escape all keys and values of an array or object
This commit is contained in:
parent
60bc1e5637
commit
91ff3c6323
2 changed files with 41 additions and 3 deletions
|
|
@ -96,6 +96,35 @@ function array_last_key(array $array)
|
|||
return key($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape all keys and values in a multi-dimensional array.
|
||||
*
|
||||
* @param array $array The array to escape
|
||||
* @param bool $double_escape Set this to false to skip symbols that are already escaped (default: true)
|
||||
* @return array
|
||||
*/
|
||||
function array_escape(array $array, $double_escape = true)
|
||||
{
|
||||
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
|
||||
$result = array();
|
||||
foreach ($array as $key => $value)
|
||||
{
|
||||
if (is_array($value))
|
||||
{
|
||||
$result[htmlspecialchars($key, $flags, 'UTF-8', $double_escape)] = array_escape($value, $double_escape, $flags);
|
||||
}
|
||||
elseif (is_object($value))
|
||||
{
|
||||
$result[htmlspecialchars($key, $flags, 'UTF-8', $double_escape)] = (object)array_escape(get_object_vars($value), $double_escape, $flags);
|
||||
}
|
||||
else
|
||||
{
|
||||
$result[htmlspecialchars($key, $flags, 'UTF-8', $double_escape)] = htmlspecialchars($value, $flags, 'UTF-8', $double_escape);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten a multi-dimensional array into a one-dimensional array.
|
||||
* Based on util.php <https://github.com/brandonwamboldt/utilphp>
|
||||
|
|
@ -155,7 +184,7 @@ function clean_path($path)
|
|||
*/
|
||||
function escape($str, $double_escape = true)
|
||||
{
|
||||
$flags = defined('ENT_SUBSTITUTE') ? (ENT_QUOTES | ENT_SUBSTITUTE) : (ENT_QUOTES | ENT_IGNORE);
|
||||
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
|
||||
return htmlspecialchars($str, $flags, 'UTF-8', $double_escape);
|
||||
}
|
||||
|
||||
|
|
@ -178,8 +207,7 @@ function escape_css($str)
|
|||
*/
|
||||
function escape_js($str)
|
||||
{
|
||||
$flags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT;
|
||||
if (defined('JSON_UNESCAPED_UNICODE')) $flags = $flags | JSON_UNESCAPED_UNICODE;
|
||||
$flags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE;
|
||||
$str = json_encode((string)$str, $flags);
|
||||
return substr($str, 1, strlen($str) - 2);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue