Download the PHP package mehr-it/html-cleaner without Composer

On this page you can find all versions of the php package mehr-it/html-cleaner. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package html-cleaner

HTML cleaner for PHP

This library aims to offer an easy API for removing unwanted elements from a given HTML fragment. This is required when outputting HTML from an untrusted source such as browsers, API clients or other third parties.

Usage

The HtmlCleaner class allows to define blacklists and whitelists for element types, tag names and attributes. If more customization is required, callbacks my be defined for filtering.

Restrict allowed tags

The following example only allows <p> and <br> tags. All other tags and their content are removed.

$cleaned = (new HtmlCleaner())
    ->setTagWhitelist(['p', 'br'])
    ->cleanFragment($html);

Instead of a whitelist, a blacklist can be used via setTagBlacklist() or even a callback which receives the tag name and must return true to keep the designated tag:

$cleaned = (new HtmlCleaner())
    ->setTagCallback(function($tag, $cleaner) {
        return $tag == 'span';
    })
    ->cleanFragment($html);

Restrict element types

HTML also contains other elements, such as comments and CDATA. They cannot be filtered by tag name, but by using the element filter functions in the same way as for tag restriction. Following example only allows tags and text nodes:

$cleaned = (new HtmlCleaner())
    ->setElementTypeWhitelist([
        HtmlCleaner::ELEMENT_TYPE_TAG,
        HtmlCleaner::ELEMENT_TYPE_TEXT,
    ])
    ->cleanFragment($html); 

Filter attributes

Even if certain tags should be allowed, some attributes might have to be removed. Following example only allows style attributes:

$cleaned = (new HtmlCleaner())
    ->setAttributeWhitelist(['style'])
    ->cleanFragment($html);

If all attributes should be removed, the blacklist with the wildcard entry '*' can be used:

 $cleaned = (new HtmlCleaner())
        ->setAttributeBlacklist(['*'])
        ->cleanFragment($html);

Replacing nodes

Imagine following HTML fragment:

<p>A big search engine is called <a href="https://www.google.com">Google</a>.</p>

Simply removing all <a> tags, would also cause their content to be removed. But what if the text should be kept? Here the replacing functionality comes in. Following example replaces all <a> tags with <span>:

$cleaned = (new HtmlCleaner())
    ->setReplacements([
        'a' => 'span'
    ])
    ->cleanFragment($html);

// output: "<p>A big search engine is called <span>Google</span>.</p>"

As you see, existing attributes are removed automatically.

To get rid of the <span> tags, you may simply pass null as value, to only keep the text content of a node:

$cleaned = (new HtmlCleaner())
    ->setReplacements([
        'a' => null
    ])
    ->cleanFragment($html);

// output: "<p>A big search engine is called Google.</p>"

You may even pass a Closure as replacement to generate a replacement value such as a tag name, null or even a newly created DOMNode. If the callback returns false the corresponding node is not replaced but removed.

Unwrapping nodes

Sometimes replacing nodes is not what you want. Often you might want to get rid of some nodes but keep their content. You may specify these nodes using the "unwrap" list:

$html = '<p>This is <b>me</b> and you</p>

$cleaned = (new HtmlCleaner())
   ->setUnwraps([
       'p',
   ])
   ->cleanFragment($html);

// output: "This is <b>me</b> and you"

You may pass '*' as a wildcard to unwrap any nodes. Note: Replacements take precedence over unwraps!

If you want to unwrap a node and prepend/append other elements, an associative array may be passed:

$html = '<p>This is <b>me</b> and you</p>

$cleaned = (new HtmlCleaner())
   ->setUnwraps([
       'p',
       'b' => ['about ', ':innerHtml', ', my life'],
   ])
   ->cleanFragment($html);

// output: "This is about me, my life and you"

The string ':innerHTML' has a special meaning and will replaced with all child nodes of the unwrapped node. Any other strings are converted to text nodes.


All versions of html-cleaner with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
masterminds/html5 Version ^2.0
ext-dom Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mehr-it/html-cleaner contains the following files

Loading the files please wait ....