Thread PHP-Script in Perl umwandeln (6 answers)
Opened by Henri at 2015-03-30 09:19

Gast Henri
 2015-03-30 09:19
#180447 #180447
Hallo leute kann jemand mir hier helfen, habe ich ein PHP-code, das ich gern in Perl umwandeln möchte

more (6.1kb):
Code: (dl )
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

/**
* @param $obj
* @return array
*/
function addObjectElementsToArray($obj)
{
$arrAllFile = array();
foreach ($obj->children() as $child) {
$arrAllFile[] = $child;

}
return $arrAllFile;
}

/**
* @param $obj1
* @param $obj2
* @return array
*/
function getDifference ($obj1, $obj2)
{
$arrDiff = array();

if (json_encode($obj1['id']) != json_encode($obj2['id'])) {
if (isset($arrDiff['Catalog']) && !is_array($arrDiff['Catalog'])) {
$arrDiff['Catalog'] = array();
}
$arrDiff['Catalog']['f1-id'] = $obj1['id'];
$arrDiff['Catalog']['f2-id'] = $obj2['id'];
}

//check version
if (json_encode($obj1['version']) != json_encode($obj2['version'])) {
if (isset($arrDiff['Catalog']) && !is_array($arrDiff['Catalog'])) {
$arrDiff['Catalog'] = array();
}
$arrDiff['Catalog']['f1-version'] = $obj1['version'];
$arrDiff['Catalog']['f2-version'] = $obj2['version'];
}
$c = count($obj1->children());

// prüf parameter
for ($j = 0; $j <= $c; $j++) {
if (isset($arrDiff['Parameter']) && !is_array($arrDiff['Parameter'])) {
$arrDiff['Parameter'] = array();
}

if(json_encode($obj1->children()[$j]['name']) != json_encode($obj2->children()[$j]['name'])) {
$arrDiff['Parameter']['f1-name-'.$j] = $obj1->children()[$j]['name'];
$arrDiff['Parameter']['f2-name-'.$j] = $obj2->children()[$j]['name'];
}

if(json_encode($obj1->children()[$j]['value']) != json_encode($obj2->children()[$j]['value'])) {
$arrDiff['Parameter']['f1-value-'.$j] = $obj1->children()[$j]['value'];
$arrDiff['Parameter']['f2-value-'.$j] = $obj2->children()[$j]['value'];
}
}

if (empty($arrDiff['Catalog']) && empty($arrDiff['Parameter'])) {
return [];
}

return $arrDiff;
}




$arrFile1 =simplexml_load_file("default_sysdumpdev_150319.txt") or die("Error: Cannot create object");
$arrFile2 =simplexml_load_file("d100spuptl25e0_sysdumpdev.txt") or die("Error: Cannot create object");


$arrAllFile1 = addObjectElementsToArray ($arrFile1);
$arrAllFile2 = addObjectElementsToArray ($arrFile2);


$arrDifferences = array();
if (count($arrAllFile1) != count($arrAllFile2)) {
$arrDifferences['differentSize'] = "Die Anzahl der Elemente in beide Dateien Stimmen nicht überein";
//exit
} else if (count($arrAllFile1 != count($arrAllFile2))) {
//loop and compare step step by step
$arrDifferenceList = array();
for ($i = 0; $i < count($arrAllFile1); $i++) {
if (json_encode($arrAllFile1[$i]) != json_encode($arrAllFile2[$i])) {
//pruf jeder element des nicht gleiche obj element
$diff = getDifference($arrAllFile1[$i], $arrAllFile2[$i]);
if (!empty($diff)) {
$arrDifferenceList[] = $diff;
}
}
}

echo "<pre>";
$arrDifferences['differences'] = $arrDifferenceList;
}


// Schreibe die unterschieden in eine result Datei
$content = '';
if (isset($arrDifferences['differentSize'])) {
$content = $arrDifferences['differentSize'] . '\n';
}

if (isset($arrDifferences['differences'])) {
print_r($arrDifferences);
foreach ($arrDifferences['differences'] as $diff) {
foreach($diff as $key => $arr) {
if ($key == 'Catalog') {
$content .= 'CATALOG:' . PHP_EOL;
foreach ($arr as $key => $value) {
$content .= $key . ' = '.$value . PHP_EOL;
}
}

if ($key == 'Parameter') {
$content .= PHP_EOL . 'PARAMETER:' . PHP_EOL;
foreach ($arr as $key => $value) {
$content .= $key . ' = '.$value . PHP_EOL;
}
}
}
}

}

if ($content == '') {
$content = 'Es git keine Unterschied in Beide Dateien';
}
// erstellt die datei result.txt oder lösche die inhalte der bereit existierende
$file = fopen("result.txt","w+");
fwrite($file,$content ,strlen($content));
fclose($file);

Last edited: 2015-03-30 11:54:09 +0200 (CEST)

View full thread PHP-Script in Perl umwandeln