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
- Host: GitHub
- URL: https://github.com/dneustadt/html-tag-replace
- Owner: dneustadt
- Created: 2017-12-30T05:46:42.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-10-22T14:49:23.000Z (over 6 years ago)
- Last Synced: 2024-11-18T09:10:17.606Z (over 1 year ago)
- Topics: attributes, dom, html, markup, replace, search, tags
- Language: PHP
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
HtmlTagReplace
=====
[](https://scrutinizer-ci.com/g/dneustadt/html-tag-replace/?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 = '
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