How can PHP developers efficiently handle inconsistent data formats, like varying spacing between numbers, when reading files into arrays?

When handling inconsistent data formats, PHP developers can use regular expressions to match and extract the desired data regardless of the spacing variations. By creating a pattern that captures the necessary information while ignoring the spaces, developers can ensure that the data is parsed correctly into arrays.

$data = file_get_contents('data.txt');
$pattern = '/(\d+)\s+(\d+)\s+(\d+)/'; // Define a pattern to match numbers with varying spacing
preg_match_all($pattern, $data, $matches, PREG_SET_ORDER);

$result = [];
foreach ($matches as $match) {
    $result[] = [
        'number1' => $match[1],
        'number2' => $match[2],
        'number3' => $match[3]
    ];
}

print_r($result);