3v4l.org

run code in 500+ PHP versions simultaneously
<?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"; ?>
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.36, 8.3.0 - 8.3.37, 8.4.1 - 8.4.28, 8.5.0 - 8.5.13, 8.6.0
Total Calibration Value: 142

preferences:
15.47 ms | 1164 KiB | 6 Q