PHPackages                             avplab/php-html-builder - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. avplab/php-html-builder

ActivePackage[Utility &amp; Helpers](/categories/utility)

avplab/php-html-builder
=======================

PHP Html builder simplifies creation of an html code in php scripts. Allows to build (or generate) the html in simple natural way, similarly as create a html page.

v2.0.3(1y ago)189.2k↓50%2MITPHPPHP &gt;=5.3.0CI failing

Since Oct 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/avplab/php-html-builder)[ Packagist](https://packagist.org/packages/avplab/php-html-builder)[ Docs](http://github.com/avplab/php-html-builder)[ RSS](/packages/avplab-php-html-builder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (8)Used By (0)

Php Html Builder
================

[](#php-html-builder)

[![Build Status](https://camo.githubusercontent.com/250d2774dae600b5a7137b434ebbaede15ddc912c9d0e2d7a72d4c3854fe05d8/68747470733a2f2f7472617669732d63692e6f72672f6176706c61622f7068702d68746d6c2d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/avplab/php-html-builder)

Sometimes, we strongly need something simple for creating html code in php runtime. For example, we want to build a simple html report, or build a simple html for highlighting some profiling data, etc. Usually for such cases we don't want to use templates engines, or create separated html files. As result we have mess of html and php code, or tons of concatenated strings with html code. PhpHtmlBuilder was created to solve these issues quickly and without clogging the php code. You simply use it same as you write a html.

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

[](#installation)

Install the component by using [Composer](https://getcomposer.org). Update your project's `composer.json` file to include dependency.

```
"require": {
    "avplab/php-html-builder": "~2.0"
}

```

Usage
-----

[](#usage)

To start building an html code, create an instance of `AvpLab\PhpHtmlBuilder` and use it similary as writing html.

```
$builder = new \AvpLab\PhpHtmlBuilder();
$builder
    ->tag('!DOCTYPE')->setHtml()->endOpened()
    ->html()->setLang('en')
        ->head()
            ->meta()->setHttpEquiv('X-UA-Compatible')->setContent('IE=edge,chrome=1')->endOpened()
            ->title('PhpHtmlBuilder: Example')->end()
        ->end()
        ->body()
            ->div()->setClass('container')
                ->div()->setClass('row')
                    ->div()->setClass('col-md-12')
                        ->h1('PhpHtmlBuilder Demo')->end()
                        ->p('Designed to make the code easier')->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end();

echo $builder;
```

The example above will build the following html:

```

    PhpHtmlBuilder: Example

          PhpHtmlBuilder Demo
          Designed to make the code easier

```

### Comments

[](#comments)

To add comment block use method `addComment()`.

```
$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder->addComment('foo');

//Result

```

### Tags

[](#tags)

There are two ways to create HTML tags: The first one(this is also the most common way) is to call the method with the same name as HTML tag in `CamelCase` format. Tags are always be converted into lowercase with dashes.

```
$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder->html()->customTag()->end()->end();

//Result

```

The second way is to call the method `tag()` with the name of the HTML tag. In this case no any conversion is applied. This is useful when you need to create very specific tags like ``.

```
$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder->tag('!DOCTYPE')->endOpened();

//Result
DOCTYPE>
```

To complete the tag need to call one of the following methods: `end()`, `endShorted()` and `endOpened()`.

- Method `end()` will create tag `` (i.e. `div`, `p`, `span`, etc.).
- Method `endShorted()` will create short tag ``. In this case, tag can have only attributes (i.e. `script`, `link`, `img` or `input` and similar).
- Method `endOpened()` will create opened tag ``. In this case, tag can have only attributes (i.e. `meta` or similar).

There is also a possibility to add html and attributes during tag creation, using arguments of methods. Arguments will be recognized in the following way:

- If only one argument is provided, and this is an array, it will be recognized as tag attributes, otherwise as tag html(see `addHtml()` below).
- If two arguments are provided, the first is a tag html, and second is array of attributes. NOTE: in this case don't need to use camelCase for attributes names. It will keep the name as is. Also if attribute doesn't have a key, it will be recognized as attribute without value.

```
$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder
    ->div('title', ['class' => 'container', 'Foo' => 'Bar', 'baz'])
        ->p(['class' => 'article'])->end()
    ->end();

//Result
title
```

#### Attributes

[](#attributes)

Creating attributes is very similar as creating tags. You need to call the method with an appropriate name in `CamelCase` format and beginning with `set`. If method called without arguments, only attribute name will be applied.

```
$builder = new \AvpLab\PhpHtmlBuilder();
$builder
    ->tag('!DOCTYPE')->setHtml()->endOpened()
    ->html()->setLang('en')->end();

//Result
DOCTYPE html>
```

#### Content

[](#content)

To add a plain text(escaped) into the tag, call the method `addText()`. For adding "raw" html string, use another method `addHtml()`.

```
$builder = new \AvpLab\PhpHtmlBuilder();
$builder
    ->div()
        ->addText('foo')
        ->addHtml('bar')
    ->end();

//Result
foobar
```

Render
------

[](#render)

To get an HTML string, you need to call the `build()` method or simply recognize it as string.

```
$builder = new \AvpLab\PhpHtmlBuilder();
// Do some html

$htmlString = $builder->build();
echo $htmlString;

// Supports string recognision
echo $builder;
```

License
=======

[](#license)

PhpHtmlBuilder is licensed under the MIT License - see the `LICENSE` file for details

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

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 ~636 days

Recently: every ~766 days

Total

6

Last Release

691d ago

Major Versions

v1.0.1 → v2.0.02017-03-07

PHP version history (3 changes)v1.0.0PHP &gt;=5.3.3

v2.0.0PHP &gt;=5.5.0

v2.0.1PHP &gt;=5.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a42c022656091cc5d65fcc2c2c063c53597b1fa60e6be84be51cd727b22097e3?d=identicon)[avplab](/maintainers/avplab)

---

Top Contributors

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

---

Tags

phphtmlbuilder

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/avplab-php-html-builder/health.svg)

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

###  Alternatives

[gherkins/regexpbuilderphp

PHP port of thebinarysearchtree/regexpbuilderjs

1.4k163.0k1](/packages/gherkins-regexpbuilderphp)[artem_c/emmet

emmet implementation for php

141.8k](/packages/artem-c-emmet)

PHPackages © 2026

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