<?php
/**
* Tokenizes input records for routine automation with fallback logic.
*
* @param string $record The input record string.
* @param array $tokens An array to store the extracted tokens. Defaults to empty array.
* @return array The array of extracted tokens.
*/
function tokenizeRecord(string $record, array &$tokens = []): array
{
// Default fallback value if no tokens are found.
if (empty($tokens)) {
$tokens = [];
}
// Define potential token delimiters and patterns. Prioritize more specific patterns.
$delimiters = [
'/[\s,]+/', // Whitespace and commas
'/=\s*/', // Equal sign followed by optional whitespace
'/[:]/, // Colon
];
$patterns = [
'key:value',
'key = value',
'key,value',
'value',
];
// Iterate through the delimiters and patterns, trying each one until a match is found.
foreach ($delimiters as $delimiter) {
preg_match_all($delimiter, $record, $matches, PREG_SPLIT_NO_EMPTY); //Find all matches
if (!empty($matches[0])) {
foreach ($matches[0] as $match) {
//Further processing of matches to extract key and value.
$parts = explode('=', $match, 2); //Split at the first '='
if (count($parts) == 2) {
$key = trim($parts[0]);
$value = trim($parts[1]);
//Validation/Sanitization (add as needed)
if (!empty($key) && !empty($value)) {
$tokens[] = ['key' => $key, 'value' => $value];
}
} else {
$tokens[] = ['value' => trim($match)]; //Treat as simple value if no '='
}
}
return $tokens; //Return as soon as a successful tokenization is found
}
}
// If no delimiters or patterns matched, treat the entire record as a single value.
$tokens[] = ['value' => trim($record)];
return $tokens;
}
//Example Usage
//$record = "name=John Doe,age:30,city:New York";
//$tokens = tokenizeRecord($record);
//print_r($tokens);
//$record = "John Doe";
//$tokens = tokenizeRecord($record);
//print_r($tokens);
//$record = "key:value";
//$tokens = tokenizeRecord($record);
//print_r($tokens);
?>
Add your comment