Virtlab:Řídící server/SupportFunctions.php.inc
Z VirtlabWiki
(Rozdíly mezi verzemi)
Verze z 21:54, 21. 2. 2007 Vav166 (Diskuse | příspěvky) ← Předchozí porovnání |
Verze z 15:37, 22. 2. 2007 Vav166 (Diskuse | příspěvky) Následující porovnání → |
||
Řádka 2: | Řádka 2: | ||
== Popis funkcí == | == Popis funkcí == | ||
- | <pre> | + | ; function ClearWhitespaceInXML($data) : ze vstupních dat (XML dat), odstraní bílé znaky mezi značkami. |
- | 3:function ClearWhitespaceInXML($data) { | + | ; function Unique($array) : z pole udělá množinu (odstraní duplicitní hodnoty) |
- | 10:function Unique($array) { | + | ; function array_delete(&$array, $value) : z pole <tt>$array</tt> odstraní hodnotu <tt>$value</tt>. |
- | 17:function array_delete(&$array, $value) { | + | ; function array_porovnej($array1, $array2) : vypočíta rozdíl multimnožin <tt>$array1 - $array2</tt> |
- | 29:function array_porovnej($array1, $array2) { | + | ; function DoubleArrayItems($array) : zdvojí prvky pole |
- | 47:function DoubleArrayItems($array) { | + | ; function MatrixClear(&$matice, $radek, $sloupec) : v zadané matici zruší určený sloupec a řádek |
- | 60:function MatrixClear(&$matice, $radek, $sloupec) { | + | |
- | </pre> | + | |
== Příklady == | == Příklady == | ||
+ | '''$xml''' = "<tag1>Hello World</tag1> | ||
+ | <tag1>Hello World</tag1> <tag1>Hello World</tag1> | ||
+ | <tag1>Hello World</tag1>"; | ||
+ | |||
+ | $cisto = '''ClearWhitespaceInXML($xml)'''; | ||
+ | |||
+ | '''$cisto''' == "<tag1>Hello World</tag1><tag1>Hello World</tag1><tag1>Hello World</tag1><tag1>Hello World</tag1>"; | ||
+ | |||
+ | $a1 = array(1, 1, 1, 2, 3, 4, 5, 5, 6, 6, 6, 6); | ||
+ | $a2 = array(1, 1, 1, 1, 2, 4, 5, 6, 6, 6); | ||
+ | $a12 = '''array_porovnej($a1, $a2)'''; | ||
+ | $a21 = '''array_porovnej($a2, $a1)'''; | ||
+ | |||
+ | '''$a1''' | ||
+ | ( | ||
+ | [0] => 1 | ||
+ | [1] => 1 | ||
+ | [2] => 1 | ||
+ | [3] => 2 | ||
+ | [4] => 3 | ||
+ | [5] => 4 | ||
+ | [6] => 5 | ||
+ | [7] => 5 | ||
+ | [8] => 6 | ||
+ | [9] => 6 | ||
+ | [10] => 6 | ||
+ | [11] => 6 | ||
+ | ) | ||
+ | |||
+ | '''$a2''' | ||
+ | ( | ||
+ | [0] => 1 | ||
+ | [1] => 1 | ||
+ | [2] => 1 | ||
+ | [3] => 1 | ||
+ | [4] => 2 | ||
+ | [5] => 4 | ||
+ | [6] => 5 | ||
+ | [7] => 6 | ||
+ | [8] => 6 | ||
+ | [9] => 6 | ||
+ | ) | ||
+ | |||
+ | '''$a12''' | ||
+ | ( | ||
+ | [4] => 3 | ||
+ | [7] => 5 | ||
+ | [11] => 6 | ||
+ | ) | ||
+ | |||
+ | '''$a21''' | ||
+ | ( | ||
+ | [3] => 1 | ||
+ | ) | ||
+ | |||
+ | $a = array(1, "index1" => 2, 3, "index2" => 4, "index3" => 5, | ||
+ | 6 => array(3, "index4" => 4, "index5" => 5)); | ||
+ | $a2 = '''DoubleArrayItems($a)'''; | ||
+ | |||
+ | '''$a''' | ||
+ | ( | ||
+ | [0] => 1 | ||
+ | [index1] => 2 | ||
+ | [1] => 3 | ||
+ | [index2] => 4 | ||
+ | [index3] => 5 | ||
+ | [6] => Array | ||
+ | ( | ||
+ | [0] => 3 | ||
+ | [index4] => 4 | ||
+ | [index5] => 5 | ||
+ | ) | ||
+ | ) | ||
+ | |||
+ | '''$a2''' | ||
+ | ( | ||
+ | [0] => 1 | ||
+ | [1] => 1 | ||
+ | [2] => 2 | ||
+ | [3] => 2 | ||
+ | [4] => 3 | ||
+ | [5] => 3 | ||
+ | [6] => 4 | ||
+ | [7] => 4 | ||
+ | [8] => 5 | ||
+ | [9] => 5 | ||
+ | [10] => Array | ||
+ | ( | ||
+ | [0] => 3 | ||
+ | [index4] => 4 | ||
+ | [index5] => 5 | ||
+ | ) | ||
+ | [11] => Array | ||
+ | ( | ||
+ | [0] => 3 | ||
+ | [index4] => 4 | ||
+ | [index5] => 5 | ||
+ | ) | ||
+ | ) | ||
+ | |||
+ | '''$a''' | ||
+ | ( | ||
+ | [radek1] => Array | ||
+ | ( | ||
+ | [sloupec1] => 0 | ||
+ | [sloupec2] => 1 | ||
+ | [sloupec3] => 2 | ||
+ | [sloupec4] => 3 | ||
+ | ) | ||
+ | [radek2] => Array | ||
+ | ( | ||
+ | [sloupec1] => 0 | ||
+ | [sloupec3] => 2 | ||
+ | [sloupec4] => 3 | ||
+ | ) | ||
+ | [radek3] => Array | ||
+ | ( | ||
+ | [sloupec3] => 2 | ||
+ | [sloupec4] => 3 | ||
+ | ) | ||
+ | ) | ||
+ | |||
+ | '''MatrixClear(&$a, "radek2", "sloupec1")'''; | ||
+ | |||
+ | '''$a''' | ||
+ | ( | ||
+ | [radek1] => Array | ||
+ | ( | ||
+ | [sloupec2] => 1 | ||
+ | [sloupec3] => 2 | ||
+ | [sloupec4] => 3 | ||
+ | ) | ||
+ | [radek3] => Array | ||
+ | ( | ||
+ | [sloupec3] => 2 | ||
+ | [sloupec4] => 3 | ||
+ | ) | ||
+ | ) | ||
== Zdrojový kód == | == Zdrojový kód == | ||
Řádka 22: | Řádka 158: | ||
return preg_replace($pattern, $replacement, $data); | return preg_replace($pattern, $replacement, $data); | ||
}//function | }//function | ||
- | |||
function Unique($array) { | function Unique($array) { | ||
Řádka 29: | Řádka 164: | ||
return NULL; | return NULL; | ||
}//function | }//function | ||
- | |||
function array_delete(&$array, $value) { | function array_delete(&$array, $value) { | ||
Řádka 41: | Řádka 175: | ||
return NULL; | return NULL; | ||
} | } | ||
- | |||
function array_porovnej($array1, $array2) { | function array_porovnej($array1, $array2) { | ||
Řádka 59: | Řádka 192: | ||
return $result; | return $result; | ||
}//function | }//function | ||
- | |||
function DoubleArrayItems($array) { | function DoubleArrayItems($array) { | ||
Řádka 72: | Řádka 204: | ||
return NULL; | return NULL; | ||
}//function | }//function | ||
- | |||
function MatrixClear(&$matice, $radek, $sloupec) { | function MatrixClear(&$matice, $radek, $sloupec) { | ||
Řádka 78: | Řádka 209: | ||
foreach($matice as $radecek => $radky) | foreach($matice as $radecek => $radky) | ||
foreach($radky as $sloupecek => $hodonota) { | foreach($radky as $sloupecek => $hodonota) { | ||
- | if($sloupecek == $sloupec) unset($matice[$radecek][$sloupecek]); | + | if($sloupecek == $sloupec) |
+ | unset($matice[$radecek][$sloupecek]); | ||
} | } | ||
}//function | }//function |
Verze z 15:37, 22. 2. 2007
Pomocné funkce.
Popis funkcí
- function ClearWhitespaceInXML($data)
- ze vstupních dat (XML dat), odstraní bílé znaky mezi značkami.
- function Unique($array)
- z pole udělá množinu (odstraní duplicitní hodnoty)
- function array_delete(&$array, $value)
- z pole $array odstraní hodnotu $value.
- function array_porovnej($array1, $array2)
- vypočíta rozdíl multimnožin $array1 - $array2
- function DoubleArrayItems($array)
- zdvojí prvky pole
- function MatrixClear(&$matice, $radek, $sloupec)
- v zadané matici zruší určený sloupec a řádek
Příklady
$xml = "<tag1>Hello World</tag1> <tag1>Hello World</tag1> <tag1>Hello World</tag1> <tag1>Hello World</tag1>"; $cisto = ClearWhitespaceInXML($xml); $cisto == "<tag1>Hello World</tag1><tag1>Hello World</tag1><tag1>Hello World</tag1><tag1>Hello World</tag1>";
$a1 = array(1, 1, 1, 2, 3, 4, 5, 5, 6, 6, 6, 6); $a2 = array(1, 1, 1, 1, 2, 4, 5, 6, 6, 6); $a12 = array_porovnej($a1, $a2); $a21 = array_porovnej($a2, $a1); $a1 ( [0] => 1 [1] => 1 [2] => 1 [3] => 2 [4] => 3 [5] => 4 [6] => 5 [7] => 5 [8] => 6 [9] => 6 [10] => 6 [11] => 6 ) $a2 ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 2 [5] => 4 [6] => 5 [7] => 6 [8] => 6 [9] => 6 ) $a12 ( [4] => 3 [7] => 5 [11] => 6 ) $a21 ( [3] => 1 )
$a = array(1, "index1" => 2, 3, "index2" => 4, "index3" => 5, 6 => array(3, "index4" => 4, "index5" => 5)); $a2 = DoubleArrayItems($a); $a ( [0] => 1 [index1] => 2 [1] => 3 [index2] => 4 [index3] => 5 [6] => Array ( [0] => 3 [index4] => 4 [index5] => 5 ) ) $a2 ( [0] => 1 [1] => 1 [2] => 2 [3] => 2 [4] => 3 [5] => 3 [6] => 4 [7] => 4 [8] => 5 [9] => 5 [10] => Array ( [0] => 3 [index4] => 4 [index5] => 5 ) [11] => Array ( [0] => 3 [index4] => 4 [index5] => 5 ) )
$a ( [radek1] => Array ( [sloupec1] => 0 [sloupec2] => 1 [sloupec3] => 2 [sloupec4] => 3 ) [radek2] => Array ( [sloupec1] => 0 [sloupec3] => 2 [sloupec4] => 3 ) [radek3] => Array ( [sloupec3] => 2 [sloupec4] => 3 ) ) MatrixClear(&$a, "radek2", "sloupec1"); $a ( [radek1] => Array ( [sloupec2] => 1 [sloupec3] => 2 [sloupec4] => 3 ) [radek3] => Array ( [sloupec3] => 2 [sloupec4] => 3 ) )
Zdrojový kód
<?php function ClearWhitespaceInXML($data) { $pattern = "/>\s+</"; $replacement = "><"; return preg_replace($pattern, $replacement, $data); }//function function Unique($array) { if(is_array($array)) return array_keys(array_flip($array)); return NULL; }//function function array_delete(&$array, $value) { if(is_array($array)) { if(in_array($value, $array)) { $array = array_diff($array, array($value)); return 1; } return 0; }//if return NULL; } function array_porovnej($array1, $array2) { $result = array(); foreach($array1 as $idx1 => $value1) { $exists = 0; foreach($array2 as $idx2 => $value2) { if($value1 == $value2) { unset($array2[$idx2]); $exists = 1; break; } //else $result[$idx1] = $value1; }//foreach if(!$exists) $result[$idx1] = $value1; }//foreach return $result; }//function function DoubleArrayItems($array) { $temp = array(); if(is_array($array)) { foreach($array as $hodnota) { array_push($temp, $hodnota); array_push($temp, $hodnota); }//foreach return $temp; }//if return NULL; }//function function MatrixClear(&$matice, $radek, $sloupec) { unset($matice[$radek]); foreach($matice as $radecek => $radky) foreach($radky as $sloupecek => $hodonota) { if($sloupecek == $sloupec) unset($matice[$radecek][$sloupecek]); } }//function ?>