@php function parseImageString($imageStr) { if (!$imageStr) return []; if (is_array($imageStr)) return $imageStr; if (str_starts_with($imageStr, '[') && str_ends_with($imageStr, ']')) { try { $cleanStr = trim($imageStr, '[]'); $decoded = json_decode('[' . $cleanStr . ']', true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { return array_filter($decoded, function($url) { return !empty(trim($url)); }); } $cleanStr = str_replace('\\', '', $cleanStr); $cleanStr = str_replace('\"', '"', $cleanStr); $imageArray = []; $current = ''; $inQuotes = false; $escapeNext = false; for ($i = 0; $i < strlen($cleanStr); $i++) { $char = $cleanStr[$i]; if ($escapeNext) { $current .= $char; $escapeNext = false; continue; } if ($char === '\\') { $escapeNext = true; continue; } if ($char === '"') { $inQuotes = !$inQuotes; continue; } if ($char === ',' && !$inQuotes) { $imageArray[] = trim($current); $current = ''; continue; } $current .= $char; } if (!empty(trim($current))) { $imageArray[] = trim($current); } $imageArray = array_map(function($url) { $url = trim($url); $url = trim($url, '"\''); return $url; }, $imageArray); return array_filter($imageArray, function($url) { return !empty($url) && $url !== '""' && $url !== "''"; }); } catch (Exception $error) { $cleanStr = trim($imageStr, '[]'); $cleanStr = str_replace('\\', '', $cleanStr); $imageArray = array_map('trim', explode(',', $cleanStr)); $imageArray = array_map(function($url) { return trim($url, '"\''); }, $imageArray); return array_filter($imageArray, function($url) { return !empty($url) && $url !== '""' && $url !== "''"; }); } } return [trim($imageStr)]; } function extractImageUrls($imageStr) { if (!$imageStr) return []; if (is_array($imageStr)) return $imageStr; if (preg_match('/^(.*?\/storage\/maintenance_findings\/)\[(.*)\]$/', $imageStr, $matches)) { $basePath = $matches[1]; $fileList = $matches[2]; $files = array_map('trim', explode(',', $fileList)); $files = array_filter($files, function($file) { return !empty(trim($file, ' \\"')); }); $urls = []; foreach ($files as $file) { $file = trim($file, ' \\"'); if (!empty($file)) { $urls[] = $basePath . $file; } } return $urls; } return parseImageString($imageStr); } @endphp