SpreadQuiz/class/Utils.php
2025-10-12 11:26:15 +02:00

35 lines
1.1 KiB
PHP

<?php
class Utils
{
public const WORKSPACE_DIR = "workspace";
public static function str2kv(string $str): array
{
preg_match_all("/([^,= ]+)=([^,= ]+)/", $str, $r);
$a = array_combine($r[1], $r[2]);
foreach ($a as &$v) {
if (is_numeric($v)) { // is it a numeric value?
if (((int)$v) == ((double)$v)) { // is it an integer?
$v = (int)$v;
} else { // is it a float?
$v = (double)$v;
}
} else if (in_array(strtolower($v), ["true", "false"]) ) { // it's a boolean
$v = $v === "true";
} else if (str_starts_with($v, '"') && str_ends_with($v, '"')) { // it's a string
$v = substr($v, 1, strlen($v) - 2); // strip leading and trailing quotes
}
}
return $a;
}
public static function str2a(string $str): array {
return explode(",", $str);
}
public static function getWorkspaceDir(): string {
return getcwd() . DIRECTORY_SEPARATOR . self::WORKSPACE_DIR;
}
}