Write a PHP program to sum of all numerical values (positive integers) embedded in a sentence

  • برمجة بي اتش بي

Write a PHP program to sum of all numerical values (positive integers) embedded in a sentence.
Sentences with positive integers are given over multiple lines. Each line is a character string containing one-byte alphanumeric characters, symbols, spaces, or an empty line. However the input is 80 characters or less per line and the sum is 10,000 or less.

الأجوبة

<?php
$sum = 0;
    $s = rtrim(fgets(STDIN));
    if (preg_match_all('/[0-9]+/', $s, $a)) {
        foreach ($a[0] as $v) {
            $sum += (int) $v;
        }
    }
echo "Sum of the numeric values: ".$sum . PHP_EOL;
?>

Sample Input:
5 apple and 10 orange are rotten in the basket

Sample Output:

Sum of the numeric values: 15
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...