This commit is contained in:
Qiang Xue
2013-03-08 17:32:19 -05:00
parent d1a5bb2ab4
commit d2fcc69b34
2 changed files with 125 additions and 122 deletions

View File

@@ -7,6 +7,7 @@
namespace yii\util;
use Yii;
use yii\base\InvalidParamException;
/**
@@ -279,4 +280,35 @@ class ArrayHelper
$args[] = &$array;
call_user_func_array('array_multisort', $args);
}
/**
* Encodes special characters in an array of strings into HTML entities.
* Both the array keys and values will be encoded if needed.
* If a value is an array, this method will also encode it recursively.
* @param array $data data to be encoded
* @param string $charset the charset that the data is using. If not set,
* [[\yii\base\Application::charset]] will be used.
* @return array the encoded data
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
*/
public static function htmlEncode($data, $charset = null)
{
if ($charset === null) {
$charset = Yii::$app->charset;
}
$d = array();
foreach ($data as $key => $value) {
if (is_string($key)) {
$key = htmlspecialchars($key, ENT_QUOTES, $charset);
}
if (is_string($value)) {
$value = htmlspecialchars($value, ENT_QUOTES, $charset);
} elseif (is_array($value)) {
$value = static::htmlEncode($value);
}
$d[$key] = $value;
}
return $d;
}
}