本文整理匯總了PHP中Json::htmlEncode方法的典型用法代碼示例。如果您正苦於以下問題:PHP Json::htmlEncode方法的具體用法?PHP Json::htmlEncode怎麽用?PHP Json::htmlEncode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Json
的用法示例。
在下文中一共展示了Json::htmlEncode方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: renderTagAttributes
/**
* Renders the HTML tag attributes.
*
* Attributes whose values are of boolean type will be treated as
* [boolean attributes](http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes).
*
* Attributes whose values are null will not be rendered.
*
* The values of attributes will be HTML-encoded using [[encode()]].
*
* The "data" attribute is specially handled when it is receiving an array value. In this case,
* the array will be "expanded" and a list data attributes will be rendered. For example,
* if `'data' => ['id' => 1, 'name' => 'leaps']`, then this will be rendered:
* `data-id="1" data-name="leaps"`.
* Additionally `'data' => ['params' => ['id' => 1, 'name' => 'leaps'], 'status' => 'ok']` will be rendered as:
* `data-params='{"id":1,"name":"leaps"}' data-status="ok"`.
*
* @param array $attributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]].
* @return string the rendering result. If the attributes are not empty, they will be rendered
* into a string with a leading white space (so that it can be directly appended to the tag name
* in a tag. If there is no attribute, an empty string will be returned.
*/
public static function renderTagAttributes($attributes)
{
if (count($attributes) > 1) {
$sorted = [];
foreach (static::$attributeOrder as $name) {
if (isset($attributes[$name])) {
$sorted[$name] = $attributes[$name];
}
}
$attributes = array_merge($sorted, $attributes);
}
$html = '';
foreach ($attributes as $name => $value) {
if (is_bool($value)) {
if ($value) {
$html .= " {$name}";
}
} elseif (is_array($value)) {
if (in_array($name, static::$dataAttributes)) {
foreach ($value as $n => $v) {
if (is_array($v)) {
$html .= " {$name}-{$n}='" . Json::htmlEncode($v) . "'";
} else {
$html .= " {$name}-{$n}=\"" . static::encode($v) . '"';
}
}
} elseif ($name === 'class') {
if (empty($value)) {
continue;
}
$html .= " {$name}=\"" . static::encode(implode(' ', $value)) . '"';
} elseif ($name === 'style') {
if (empty($value)) {
continue;
}
$html .= " {$name}=\"" . static::encode(static::cssStyleFromArray($value)) . '"';
} else {
$html .= " {$name}='" . Json::htmlEncode($value) . "'";
}
} elseif ($value !== null) {
$html .= " {$name}=\"" . static::encode($value) . '"';
}
}
return $html;
}