-
Notifications
You must be signed in to change notification settings - Fork 1
/
extracting_data_from_xml_line.php
83 lines (71 loc) · 2.26 KB
/
extracting_data_from_xml_line.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
83
<?php
// assumptions
// changesets are formatted as follows:
// either (1)
// line begins with
// "<changeset" and ends with "/>"
// and it is a changeset without tags
// or (2)
// line begins with '<changeset' and ends with '">'
// tags, one in each line follows
// ends with line including '</changeset>' as sole nonwhitespace text
// applies
function value_of_key($line, $tag) {
$left_stripped = str_replace("<tag k=\"" . $tag . "\" v=\"", "", $line);
return str_replace('"/>', '', $left_stripped);
}
function quest_tag_to_identifier($line) {
return value_of_key($line, "StreetComplete:quest_type");
}
function created_by_tag_to_identifier($line) {
return value_of_key($line, "created_by");
}
// from https://www.php.net/manual/en/function.substr-compare.php
function str_begins($haystack, $needle) {
return 0 === substr_compare($haystack, $needle, 0, strlen($needle));
}
function str_ends($haystack, $needle) {
return 0 === substr_compare($haystack, $needle, -strlen($needle));
}
function contains_substr($mainStr, $str, $loc = false) {
if ($loc === false) return (strpos($mainStr, $str) !== false);
if (strlen($mainStr) < strlen($str)) return false;
if (($loc + strlen($str)) > strlen($mainStr)) return false;
return (strcmp(substr($mainStr, $loc, strlen($str)), $str) == 0);
}
function get_changes_number($changeset_header) {
if (preg_match("/num_changes=\"([0-9]+)\"/", $changeset_header, $matches)) {
return (int)$matches[1];
} else {
return 0;
}
}
function get_quest_type($changeset_header) {
if (preg_match("/v=\"([^\"]+)\"/", $changeset_header, $matches)) {
return $matches[1];
} else {
return NULL;
}
}
function get_changeset_id($changeset_header) {
if (preg_match("/ id=\"([0-9]+)\"/", $changeset_header, $matches)) {
return (int)$matches[1];
} else {
return -1;
}
}
function get_uid($changeset_header) {
if (preg_match("/ uid=\"([0-9]+)\"/", $changeset_header, $matches)) {
return (int)$matches[1];
} else {
return -1;
}
}
function get_changeset_creation_date($changeset_header) {
if (preg_match("/ created_at=\"([^\"]+)\"/", $changeset_header, $matches)) {
return $matches[1];
} else {
return -1;
}
}
?>