Advertisement
Guest User

Untitled

a guest
Dec 24th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. <?php
  2. if (isset($_GET['text'], $_GET['keys'])) {
  3. $textString = $_GET['text'];
  4. $keysString = $_GET['keys'];
  5.  
  6. $keyPattern = "/([A-z]*_?)\d+.*\d([A-z]*_?)/";
  7. preg_match_all($keyPattern, $keysString, $keyMatches);
  8.  
  9. $start = $keyMatches[1][0];
  10. $end = $keyMatches[2][0];
  11.  
  12. $textPattern = "/$start?[a-z]*(\d+\.?\d+)$end/";
  13. preg_match_all($textPattern, $textString, $textMatches);
  14.  
  15. $sum = 0;
  16.  
  17. if (empty($start) || empty($end)) {
  18. echo "<p>A key is missing</p>";
  19. } else {
  20. for ($i = 0; $i < count($textMatches[1]); $i++) {
  21. $sum += $textMatches[1][$i];
  22. }
  23. if ($sum == 0) {
  24. echo "<p>The total value is: <em>nothing</em></p>";
  25. } else {
  26. echo "<p>The total value is: <em>$sum</em></p>";
  27. }
  28. }
  29. }
  30. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement