1. <?php
  2. /**
  3. * Parses header metadata for data migration.
  4. *
  5. * @param string $header_metadata The raw header metadata string.
  6. * @return array An associative array containing parsed header information.
  7. */
  8. function parseHeaderMetadata(string $header_metadata): array
  9. {
  10. $metadata = [];
  11. // Split the metadata string into individual header entries.
  12. $entries = explode("\n", $header_metadata);
  13. foreach ($entries as $entry) {
  14. // Trim whitespace from the entry.
  15. $entry = trim($entry);
  16. // Skip empty lines.
  17. if (empty($entry)) {
  18. continue;
  19. }
  20. // Split the entry into key-value pairs.
  21. $parts = explode(":", $entry, 2); // Limit to 2 parts to handle values with colons.
  22. if (count($parts) === 2) {
  23. $key = trim($parts[0]); // Key
  24. $value = trim($parts[1]); // Value
  25. // Add the key-value pair to the metadata array.
  26. $metadata[$key] = $value;
  27. } else {
  28. // Handle malformed entries (optional). Log or throw an error.
  29. error_log("Malformed header entry: " . $entry);
  30. }
  31. }
  32. return $metadata;
  33. }
  34. // Example Usage (for testing):
  35. /*
  36. $header_metadata = <<<EOT
  37. Name: customer_id
  38. Email: user@example.com
  39. Created: 2023-10-27
  40. LastModified: 2023-10-28
  41. EOT;
  42. $parsed_data = parseHeaderMetadata($header_metadata);
  43. print_r($parsed_data);
  44. */
  45. ?>

Add your comment