-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
46 lines (34 loc) · 1.29 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace HtmlDomParser;
require_once('src/HtmlDomParser.php');
$dom = new HtmlDomParser();
$html = file_get_contents('test.php');
$dom->loadHTML($html);
$selector = '.find-class';
$elements = $dom->find($selector);
foreach ($elements as $k => $element){
$htmlContent = "<p>{$k} - Content replaced dynamically for this element.</p>";
// List of all attributes of this element
$attrs = $element->getAttributes();
foreach ($attrs as $attrK => $attr){
// Validate the property name and value
if($attr->name == 'id' && $attr->value == 'customHeading'){
$htmlContent = '<h1>Custom heading replaced dynamically.</h1>';
}
if($attr->name == 'data-replace' && $attr->value == 'true'){
$htmlContent = '<p>The content of this paragraph is dynamically replaced.</p>';
}
}
// Set and get innerHtml
//$element->innerHTML = $htmlContent;
//echo $element->innerHTML;
// Set and get outerHtml
$element->outerHTML = $htmlContent;
//echo $element->outerHTML;
// Or you can also replace innerHtml and/or outerHtml of this element via main DOM object as well
//$dom->outerHtml($element, '<h1>New Html Code H1111</h1>');
//$dom->innerHTML($element, '<h1>New Html Code H1</h1>');
}
$html = $dom->saveHTML();
$dom->clearLibXmlErrors();
echo $html;