You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a helper to replace and enhance html tags and their attributes.
4
+
5
+
## Features
6
+
7
+
* Append, prepend and switch tags.
8
+
* Add custom attributes to tags
9
+
* Switch, clone or remove attributes.
10
+
11
+
## Usage
12
+
13
+
Create an Instance of `HtmlTagReplace` passing existing markup.
14
+
15
+
Use `replaceTag` method of said instance passing the following arguments:
16
+
17
+
*`search` Name of the tag to be replaced
18
+
*`replace` Name of the new tag
19
+
*`closingTag` (bool, default: false) defines whether searched tag has closing tag
20
+
*`argumentsReplace` (array) key-value pairs (`search => replace`) of attributes to be replaced. Multidimensional (`search => array`) to clone value into multiple arguments.
21
+
*`arguments` custom arguments injected
22
+
*`append` injected after targeted tag
23
+
*`prepend` injected before targeted tag
24
+
25
+
You can call the method `compress` to minify the markup.
26
+
27
+
Finally retrieve the altered markup calling `getMarkup`
28
+
29
+
## Example
30
+
31
+
```php
32
+
$markup = '
33
+
<imgsrc="#"alt="foo">
34
+
<imgsrc="#">
35
+
<divid="foo">bar</div>
36
+
<emclass="foo">bar</em>
37
+
<inputtype="text"name="foo">
38
+
';
39
+
40
+
$replacer = new HtmlTagReplace($markup);
41
+
42
+
echo $replacer->replaceTag(
43
+
'img',
44
+
'a',
45
+
false,
46
+
['src' => 'href', 'alt' => false],
47
+
'title="show image"',
48
+
'show image</a>'
49
+
)->replaceTag(
50
+
'div',
51
+
'article',
52
+
true,
53
+
['id' => 'class'],
54
+
null,
55
+
null,
56
+
'<hr>'
57
+
)->replaceTag(
58
+
'em',
59
+
'strong',
60
+
true
61
+
)->replaceTag(
62
+
'input',
63
+
'input',
64
+
false,
65
+
['name' => ['name', 'id']]
66
+
)->compress()->getMarkup();
67
+
```
68
+
69
+
will result in (not minified for readability):
70
+
71
+
```html
72
+
<atitle="show image"href="#">show image</a>
73
+
<atitle="show image"href="#">show image</a>
74
+
<hr><articleclass="foo">bar</article>
75
+
<strongclass="foo">bar</strong>
76
+
<inputtype="text"name="foo"id="foo">
77
+
```
78
+
79
+
# Todos
80
+
81
+
* add more filter options for targeting tags
82
+
* optimize method for filtering and replacing arguments
0 commit comments