-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_bad_links.php
More file actions
54 lines (42 loc) · 1.24 KB
/
remove_bad_links.php
File metadata and controls
54 lines (42 loc) · 1.24 KB
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
47
48
49
50
51
52
53
54
<?php
$files = glob("./sytycc/OEBPS/*.xhtml");
foreach ($files as $file) {
echo "Loading $file\n";
$html = new DOMDocument();
$html->load($file);
//strip for all those odd "tag" attributes...
$all_items = $html->getElementsByTagName("*");
$updated = false;
foreach ($all_items as $item) {
if ($item->hasAttribute("tag")) {
$item->removeAttribute("tag");
$updated = true;
}
}
$links = $html->getElementsByTagName("a");
if ($links->length == 0) {
continue;
} else {
foreach ($links as $link) {
if ($link->hasAttribute("href")) {
$url_to_check = explode("#",$link->getAttribute("href"));
$filecheck = $url_to_check[0];
if (!file_exists("./sytycc/OEBPS/" . $filecheck)) {
echo "In $file the link to $filecheck did not exist -- replacing\n";
$updated = true;
$div = $html->createElement("div");
$div->setAttribute("style","display:inline-block;");
$div->setAttribute("class",$link->getAttribute("class"));
$link->parentNode->replaceChild($div,$link);
}
//echo "In $file we have a link with source: " . basename($link->getAttribute("href")) . "\n";
}
}
if ($updated) {
file_put_contents($file,$html->saveXML());
//echo $html->saveXML();
//die();
}
}
}
?>