Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcaskill/php-html-build-attributes
Generate a string of HTML attributes.
https://github.com/mcaskill/php-html-build-attributes
html php utility utility-function
Last synced: 13 days ago
JSON representation
Generate a string of HTML attributes.
- Host: GitHub
- URL: https://github.com/mcaskill/php-html-build-attributes
- Owner: mcaskill
- License: mit
- Created: 2021-03-02T16:22:32.000Z (over 3 years ago)
- Default Branch: 1.x
- Last Pushed: 2023-08-10T02:11:54.000Z (about 1 year ago)
- Last Synced: 2024-10-04T18:47:36.021Z (about 1 month ago)
- Topics: html, php, utility, utility-function
- Language: PHP
- Homepage:
- Size: 28.3 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# html_build_attributes
> PHP 5 >= 5.4, PHP 7, PHP 8
Generate a string of HTML attributes.
## Installation
Using [Composer](https://getcomposer.org/):
```
$ 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](https://semver.org/), which means
breaking changes may occur between major releases.## API
```php
html_build_attributes( array|object $attr [, callable $callback = null ] ) : string
```### Parameters
- `attr` — Associative array or object containing properties, representing
attribute names and values.
If `attr` is 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 [`Closure`][class.closure] or
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()`][function.json_encode].
- `callback` — Callback function to escape the values for HTML attributes.
If no function is provided, [`htmlspecialchars()`][function.htmlspecialchars]
is used;
If using WordPress, the [`esc_attr()`][wp.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()
```php
$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 '';
```The above example will output:
```html
```
[class.closure]: https://php.net/class.closure
[function.htmlspecialchars]: https://php.net/function.htmlspecialchars
[function.json_encode]: https://php.net/function.json_encode
[object.invoke]: https://php.net/oop5.magic#object.invoke
[wp.esc_attr]: https://developer.wordpress.org/reference/functions/esc_attr/