11# html_build_attributes
22
3- (PHP 5 >= 5.4, PHP 7, PHP 8)
4- ` html_build_attributes ` — Generate a string of HTML attributes.
3+ > PHP 5 >= 5.4, PHP 7, PHP 8
54
6- ## Description
5+ Generate a string of HTML attributes.
6+
7+ ## Installation
8+
9+ Using [ Composer] ( https://getcomposer.org/ ) :
710
8- ``` php
9- string html_build_attributes( array $attr [, callable $callback = null ] )
1011```
12+ $ composer require mcaskill/php-html-build-attributes
13+ ```
14+
15+ Alternatively, download ` Function.HTML-Build-Attributes.php ` from the package
16+ source and save the file into your project path somewhere.
17+
18+ ## Upgrading
19+
20+ This package follows [ semantic versioning] ( https://semver.org/ ) , which means
21+ breaking changes may occur between major releases.
1122
12- Generate a string of HTML attributes from the associative array provided.
23+ ## API
1324
14- ## Parameters
25+ ``` php
26+ html_build_attributes( array|object $attr [, callable $callback = null ] ) : string
27+ ```
28+
29+ ### Parameters
1530
16- - ` attr ` — Associative array, or object containing properties, representing
31+ - ` attr ` — Associative array or object containing properties, representing
1732 attribute names and values.
1833
1934 If ` attr ` is a non-iterable object, then only accessible non-static properties
@@ -39,30 +54,41 @@ Generate a string of HTML attributes from the associative array provided.
3954
4055 Any other value will be serialized using [ ` json_encode() ` ] [ function.json_encode ] .
4156
42- - ` callback ` — Callback function for escaping the values for the HTML attributes.
57+ - ` callback ` — Callback function to escape the values for HTML attributes.
4358
44- If no sanitizer is provided, [ ` htmlspecialchars() ` ] [ function.htmlspecialchars ]
59+ If no function is provided, [ ` htmlspecialchars() ` ] [ function.htmlspecialchars ]
4560 is used;
4661
4762 If using WordPress, the [ ` esc_attr() ` ] [ wp.esc_attr ] function is used.
4863
49- ## Return Values
64+ ### Return Values
5065
5166Returns a string of HTML attributes or a empty string if ` attr ` is invalid or empty.
5267
53- ## Installation
68+ ## Examples
5469
55- ### With Composer
70+ ### Example # 1 : Simple usage of html_build_attributes()
5671
57- ```
58- $ composer require mcaskill/php-html-build-attributes
72+ ``` php
73+ $attr = [
74+ 'type' => 'file',
75+ 'id' => 'avatar',
76+ 'name' => 'avatar',
77+ 'class' => [ 'form-control', 'form-control-sm' ],
78+ 'multiple' => true,
79+ 'disabled' => false,
80+ 'accept' => implode(',', [ 'image/png', 'image/jpeg' ]),
81+ 'data-max-files' => 3,
82+ ];
83+
84+ echo '<input ' . html_build_attributes($attr) . ' >';
5985```
6086
61- ### Without Composer
87+ The above example will output:
6288
63- Why are you not using [ composer ] ( http://getcomposer.org/ ) ?
64- Download ` Function.HTML-Build-Attributes.php ` from the gist and save the file
65- into your project path somewhere.
89+ ``` html
90+ < input type = " file " id = " avatar " name = " avatar " class = " form-control form-control-sm " multiple accept = " image/png,image/jpeg " data-max-files = " 3 " >
91+ ```
6692
6793[ class.closure ] : https://php.net/class.closure
6894[ function.htmlspecialchars ] : https://php.net/function.htmlspecialchars
0 commit comments