PHPackages                             mcaskill/php-html-build-attributes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. mcaskill/php-html-build-attributes

ActiveLibrary[Templating &amp; Views](/categories/templating)

mcaskill/php-html-build-attributes
==================================

Generate a string of HTML attributes.

v1.4.0(10mo ago)832.6k—0%1[1 issues](https://github.com/mcaskill/php-html-build-attributes/issues)6MITPHPPHP &gt;=7.1.0

Since Feb 23Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/mcaskill/php-html-build-attributes)[ Packagist](https://packagist.org/packages/mcaskill/php-html-build-attributes)[ RSS](/packages/mcaskill-php-html-build-attributes/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (7)Used By (6)

html\_build\_attributes
=======================

[](#html_build_attributes)

> PHP 7 &gt;= 7.1, PHP 8

Generate a string of HTML attributes.

Installation
------------

[](#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
---------

[](#upgrading)

This package follows [semantic versioning](https://semver.org/), which means breaking changes may occur between major releases.

API
---

[](#api)

```
html_build_attributes( array|object $attr [, ?callable $callback = null ] ) : string
```

### Parameters

[](#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`](https://php.net/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()`](https://php.net/function.json_encode).
- `callback` — Callback function to escape the values for HTML attributes.

    If no function is provided, [`htmlspecialchars()`](https://php.net/function.htmlspecialchars)is used;

    If using WordPress, the [`esc_attr()`](https://developer.wordpress.org/reference/functions/esc_attr/) function is used.

### Return Values

[](#return-values)

Returns a string of HTML attributes or a empty string if `attr` is invalid or empty.

Examples
--------

[](#examples)

### Example #1: Simple usage of html\_build\_attributes()

[](#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 '';
```

The above example will output:

```

```

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance52

Moderate activity, may be stable

Popularity34

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~511 days

Recently: every ~382 days

Total

7

Last Release

304d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.4.0

1.x-devPHP &gt;=7.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0a4f39523b4b2837562ba0848a0327b8d340118d1ba87cb0f5d59b1d5cb6beba?d=identicon)[mcaskill](/maintainers/mcaskill)

---

Top Contributors

[![mcaskill](https://avatars.githubusercontent.com/u/29353?v=4)](https://github.com/mcaskill "mcaskill (42 commits)")

---

Tags

htmlphputilityutility-functionhtmltemplatingfront-end

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/mcaskill-php-html-build-attributes/health.svg)

```
[![Health](https://phpackages.com/badges/mcaskill-php-html-build-attributes/health.svg)](https://phpackages.com/packages/mcaskill-php-html-build-attributes)
```

###  Alternatives

[twig/twig

Twig, the flexible, fast, and secure template language for PHP

8.4k443.2M5.8k](/packages/twig-twig)[phpoffice/phpword

PHPWord - A pure PHP library for reading and writing word processing documents (OOXML, ODF, RTF, HTML, PDF)

7.6k34.7M186](/packages/phpoffice-phpword)[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[smarty/smarty

Smarty - the compiling PHP template engine

2.3k39.1M395](/packages/smarty-smarty)[laminas/laminas-view

Fast and type safe HTML templating library with a flexible plugin system supporting multistep template composition

7526.3M230](/packages/laminas-laminas-view)[twig/string-extra

A Twig extension for Symfony String

22046.0M133](/packages/twig-string-extra)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
