Advertisement
tockata

Untitled

Dec 23rd, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. <?php
  2.     $keysCount = preg_match('/(^[A-Za-z\_]+)\d.*\d([A-Za-z\_]+)/', trim($_GET['keys']), $keys);
  3.     $text = $_GET['text'];
  4.     //var_dump($keys);
  5.     if (count($keys) > 2) {
  6.         $pattern = '/' . $keys[1] . '(.+?)' . $keys[2] . '/';
  7.         preg_match_all($pattern, $text, $resultArr);
  8.         $numericFound;
  9.         $sum = 0;
  10.         foreach ($resultArr[1] as $match) {
  11.             if (is_numeric($match)) {
  12.                 $sum += floatval(trim($match));
  13.                 $numericFound = TRUE;
  14.             }
  15.         }
  16.  
  17.         if ($numericFound) {
  18.             echo '<p>The total value is: <em>' . $sum . '</em></p>';
  19.         } elseif (!$numericFound || $sum == 0) {
  20.             echo '<p>The total value is: <em>nothing</em></p>';
  21.         }
  22.     } else {
  23.         echo '<p>A key is missing</p>';
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement