An open API service indexing awesome lists of open source software.

https://github.com/dneustadt/html-tag-replace

Replace and enhance markup tags and/or attributes
https://github.com/dneustadt/html-tag-replace

attributes dom html markup replace search tags

Last synced: about 1 year ago
JSON representation

Replace and enhance markup tags and/or attributes

Awesome Lists containing this project

README

          

HtmlTagReplace
=====
[![Scrutinizer](https://scrutinizer-ci.com/g/dneustadt/html-tag-replace/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/dneustadt/html-tag-replace/?branch=master)
[![Travis CI](https://travis-ci.org/dneustadt/html-tag-replace.svg?branch=master)](https://travis-ci.org/dneustadt/html-tag-replace)

a helper to replace and enhance html tags and their attributes.

## Features

* Append, prepend and switch tags.
* Add custom attributes to tags
* Switch, clone or remove attributes.

## Usage

Create an Instance of `HtmlTagReplace` passing existing markup.

Use `replaceTag` method of said instance passing the following arguments:

* `search` Name of the tag to be replaced
* `replace` Name of the new tag
* `closingTag` (bool, default: false) defines whether searched tag has closing tag
* `argumentsReplace` (array) key-value pairs (`search => replace`) of attributes to be replaced. Multidimensional (`search => array`) to clone value into multiple arguments.
* `arguments` custom arguments injected
* `append` injected after targeted tag
* `prepend` injected before targeted tag

You can call the method `compress` to minify the markup.

Finally retrieve the altered markup calling `getMarkup`

## Example

```php
$markup = '
foo

bar

bar

';

$replacer = new HtmlTagReplace($markup);

echo $replacer->replaceTag(
'img',
'a',
false,
['src' => 'href', 'alt' => false],
'title="show image"',
'show image'
)->replaceTag(
'div',
'article',
true,
['id' => 'class'],
null,
null,
'
'
)->replaceTag(
'em',
'strong',
true
)->replaceTag(
'input',
'input',
false,
['name' => ['name', 'id']]
)->compress()->getMarkup();
```

will result in (not minified for readability):

```html
show image
show image


bar
bar

```

# Todos

* add more filter options for targeting tags
* optimize method for filtering and replacing arguments
* content manipulation
* synchronized replacement