<?php function extractCalibrationValues($lines) { $totalCalibration = 0; foreach ($lines as $line) { $calibrationValue = getCalibrationValue($line); $totalCalibration += $calibrationValue; } return $totalCalibration; } function getCalibrationValue($line) { // Use regular expression to find the first and last digits in the line preg_match('/\d/', $line, $matchesFirstDigit); preg_match_all('/\d(?=\D*$)/', $line, $matchesLastDigit); // Combine the first and last digits to form a two-digit number $calibrationValue = (int)($matchesFirstDigit[0] . end($matchesLastDigit[0])); return $calibrationValue; } // Example lines $lines = [ "1abc2", "pqr3stu8vwx", "a1b2c3d4e5f", "treb7uchet", ]; // Calculate and output the total calibration value $totalCalibration = extractCalibrationValues($lines); echo "Total Calibration Value: $totalCalibration\n"; ?>
You have javascript disabled. You will not be able to edit any code.