-
Notifications
You must be signed in to change notification settings - Fork 6
/
functions.php
82 lines (64 loc) · 1.95 KB
/
functions.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
function node2bbcode(&$doc, $oldnode, $attributes, $startbb, $endbb, $reverse = false)
{
do {
$done = node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb, $reverse);
} while ($done);
}
function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb, $reverse)
{
$savestart = str_replace('$', '%', $startbb);
$replace = false;
$xpath = new DomXPath($doc);
$list = $xpath->query("//".$oldnode);
foreach ($list as $oldNode) {
$attr = array();
if ($oldNode->attributes->length)
foreach ($oldNode->attributes as $attribute)
$attr[$attribute->name] = $attribute->value;
$replace = true;
$startbb = $savestart;
$i = 0;
foreach ($attributes as $attribute => $value) {
$startbb = str_replace('%'.++$i, '$1', $startbb);
if (strpos('*'.$startbb, '$1') > 0) {
if ($replace and ($attr[$attribute] != '')) {
$startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count);
if ($count == 0)
$replace = false;
} else
$replace = false;
} else {
if ($attr[$attribute] != $value)
$replace = false;
}
}
if ($replace) {
if (!$reverse) {
$StartCode = $oldNode->ownerDocument->createTextNode($startbb);
$EndCode = $oldNode->ownerDocument->createTextNode($endbb);
} else {
$StartCode = $oldNode->ownerDocument->createTextNode($endbb);
$EndCode = $oldNode->ownerDocument->createTextNode($startbb);
}
$oldNode->parentNode->insertBefore($StartCode, $oldNode);
if ($oldNode->hasChildNodes()) {
foreach ($oldNode->childNodes as $child) {
$newNode = $child->cloneNode(true);
$oldNode->parentNode->insertBefore($newNode, $oldNode);
}
}
$oldNode->parentNode->insertBefore($EndCode, $oldNode);
$oldNode->parentNode->removeChild($oldNode);
}
}
return($replace);
}
function deletenode(&$doc, $node)
{
$xpath = new DomXPath($doc);
$list = $xpath->query("//".$node);
foreach ($list as $child)
$child->parentNode->removeChild($child);
}
?>