mcaskill/php-html-build-attributes
最新稳定版本:v1.4.0
Composer 安装命令:
composer require mcaskill/php-html-build-attributes
包简介
Generate a string of HTML attributes.
README 文档
README
PHP 7 >= 7.1, PHP 8
Generate a string of HTML attributes.
Installation
Using Composer:
$ composer require mcaskill/php-html-build-attributes Alternatively, download Function.HTML-Build-Attributes.php from the package source and save the file into your project path somewhere.
Upgrading
This package follows semantic versioning, which means breaking changes may occur between major releases.
API
html_build_attributes( array|object $attr [, ?callable $callback = null ] ) : string
Parameters
-
attr— Associative array or object containing properties, representing attribute names and values.If
attris a non-iterable object, then only accessible non-static properties will be incorporated into the result.If an attribute name is an empty string, the attribute is ignored. The attribute name will be trimmed of leading/trailing whitespace.
If an attribute value is callable (either as a
Closureor invokable), it is called and the returned value continues processing.If an attribute value is
null, the attribute is ignored.If an attribute value is an arrayable or a stringable object, it is converted to its primitive type.
If an attribute value is a boolean and
true, the attribute will be rendered without a value, otherwise the attribute is ignored.If an attribute value is an array, only numbers and strings are accepted. Strings of the array will be trimmed of leading/trailing whitespace. If the filtered array is empty, the attribute is ignored.
Any other value will be serialized using
json_encode(). -
callback— Callback function to escape the values for HTML attributes.If no function is provided,
htmlspecialchars()is used;If using WordPress, the
esc_attr()function is used.
Return Values
Returns a string of HTML attributes or a empty string if attr is invalid or empty.
Examples
Example #1: Simple usage of html_build_attributes()
$attr = [ 'type' => 'file', 'id' => 'avatar', 'name' => 'avatar', 'class' => [ 'form-control', 'form-control-sm' ], 'multiple' => true, 'disabled' => false, 'accept' => implode(',', [ 'image/png', 'image/jpeg' ]), 'data-max-files' => 3, ]; echo '<input ' . html_build_attributes($attr) . '>';
The above example will output:
<input type="file" id="avatar" name="avatar" class="form-control form-control-sm" multiple accept="image/png,image/jpeg" data-max-files="3">
统计信息
- 总下载量: 31.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 3
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04