49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Hilfsmethoden für verschiedene Aufgaben
|
|
*/
|
|
class Util
|
|
{
|
|
/**
|
|
* Entfernt alle Zeichen außer A-Z, a-z, 0-9 sowie - und _
|
|
* @param string $string Eingabe mit möglicherweise ungültigen Zeichen
|
|
* @return string Eingabestring mit allen ungültigen Zeichen entfernt
|
|
*/
|
|
static function removeIllegalCharacters(string $string): string
|
|
{
|
|
return preg_replace("/[^a-zA-Z0-9_-]/", "", $string);
|
|
}
|
|
|
|
static function readFileContent(string $filename): string|null
|
|
{
|
|
if (!file_exists($filename)) {
|
|
return null;
|
|
}
|
|
$file = fopen($filename, "r");
|
|
if (!$file) {
|
|
return null;
|
|
}
|
|
$fileContent = fread($file, filesize($filename));
|
|
if (!$fileContent) {
|
|
return null;
|
|
}
|
|
fclose($file);
|
|
|
|
return $fileContent;
|
|
}
|
|
|
|
/**
|
|
* Öffnet eine Datei und gibt JSON-Inhalte als Array zurück
|
|
* @param string $filename Dateipfad
|
|
* @return mixed Array mit Key-Value Paaren
|
|
*/
|
|
static function parseJsonFromFile(string $filename): mixed
|
|
{
|
|
$content = self::readFileContent($filename);
|
|
if (!isset($content)) {
|
|
return null;
|
|
}
|
|
return json_decode($content);
|
|
}
|
|
} |