|
| |
1 |
|
# string $tagFn : function to call for each tag. This function |
|
| |
2 |
|
# is called for *every* tag, opening and closing. |
|
| |
3 |
|
# It would be easy to change this to call a |
|
| |
4 |
|
# different function for open and close tags. |
|
| |
5 |
|
# The tagFn has these arguments: |
|
| |
6 |
|
# function tagFn($tag, $attribs) |
|
| |
7 |
|
# string $tag : the tag |
|
| |
8 |
|
# string $attribs : all the attributes, as one long string |
|
| |
9 |
|
# wouldn't be hard to change this to an assoc array |
|
| |
10 |
|
# string $dataFn : function to call for data (non-tag). This function |
|
| |
11 |
|
# is called for all the data in between tags, including |
|
| |
12 |
|
# comments. The dataFn has these arguments: |
|
| |
13 |
|
# function dataFn($data) |
|
| |
14 |
|
# string $data : the text |
|
| |
15 |
|
# string $data : the html to parse |
|
| |
16 |
|
|
|
| |
17 |
|
function html_parse($tagFn, $dataFn, $data) |
|
| |
18 |
|
{ |
|
| |
19 |
|
$comment = false; |
|
| |
20 |
|
|
|
| |
21 |
|
# Split on '<', so the beginning of each array entry |
|
| |
22 |
|
# will be a tag. |
|
| |
23 |
|
$TagLine = explode('<', $data); |
|
| |
24 |
|
|
|
| |
25 |
|
# Loop through each entry -- each entry is a tag |
|
| |
26 |
|
# followed by everything up to the next tag. |
|
| |
27 |
|
foreach ($TagLine as $l) { |
|
| |
28 |
|
# If we're not in a comment block, then check if |
|
| |
29 |
|
# one starts here. |
|
| |
30 |
|
if (!$comment) { |
|
| |
31 |
|
if (substr(ltrim($l), 0, 3) == "!--") { |
|
| |
32 |
|
# this is the beginning of a comment, not a tag |
|
| |
33 |
|
$comment = true; |
|
| |
34 |
|
$commentline = ""; |
|
| |
35 |
|
} |
|
| |
36 |
|
} |
|
| |
37 |
|
|
|
| |
38 |
|
# If we're in a comment block, add this entry to |
|
| |
39 |
|
# the comment block, and check if it ends here. |
|
| |
40 |
|
if ($comment) { |
|
| |
41 |
|
$commentline .= "<".$l; |
|
| |
42 |
|
if (strstr($l, "-->")) { |
|
| |
43 |
|
$line = $commentline; |
|
| |
44 |
|
$tag = ""; |
|
| |
45 |
|
$comment = false; |
|
| |
46 |
|
} |
|
| |
47 |
|
else { |
|
| |
48 |
|
$line = ""; |
|
| |
49 |
|
$tag = ""; |
|
| |
50 |
|
} |
|
| |
51 |
|
} |
|
| |
52 |
|
# Otherwise, split on '>' to separate the tag from |
|
| |
53 |
|
# the data. |
|
| |
54 |
|
else { |
|
| |
55 |
|
list($tag, $line) = explode('>', $l, 2); |
|
| |
56 |
|
} |
|
| |
57 |
|
|
|
| |
58 |
|
# If there's a tag, call the tag function |
|
| |
59 |
|
if (strlen($tag)) { |
|
| |
60 |
|
$tag = trim($tag); |
|
| |
61 |
|
list($tag, $attribline) = explode(' ', $tag, 2); |
|
| |
62 |
|
|
|
| |
63 |
|
# I could split $attribline into an |
|
| |
64 |
|
# associative array, but I don't need |
|
| |
65 |
|
# that functionality now. |
|
| |
66 |
|
$tagFn($tag, $attribline); |
|
| |
67 |
|
} |
|
| |
68 |
|
|
|
| |
69 |
|
# If there's data, call the data function |
|
| |
70 |
|
if (strlen($line)) { |
|
| |
71 |
|
$dataFn($line); |
|
| |
72 |
|
} |
|
| |
73 |
|
} |
|
| |
74 |
|
} |
|
| |
75 |
|
|