Posted By

fergc on 06/28/12


Tagged


Versions (?)

getCode


 / Published in: PHP
 

devuelve 4 digitos de los puntos

  1. private function getCode($s, $flag = false)
  2. {
  3. $retorno = '';
  4. $temp;
  5. $n;
  6. if (!$s || $s == '' || self::hasLetters($s) || $flag)
  7. {
  8. $retorno = '29871';
  9. }
  10. else
  11. {
  12. $n = strlen($s);
  13. if ($n > 9)
  14. {
  15. $temp = (string)$n;
  16. $n = (int)$temp[0] + (int)$temp[1];
  17. }
  18. $retorno .= (string)$n;
  19. ((int)substr($s, -1, 1) % 2 == 0)? $retorno .= '0' : $retorno .= '1';
  20. $n = 0;
  21. $temp = $s;
  22. for ($i = 0; $i < strlen($temp); $i++)
  23. {
  24. $n += (int)$temp[$i];
  25. }
  26. if ($n > 9)
  27. {
  28. $temp = (string)$n;
  29. $n = (int)$temp[0] + (int)$temp[1];
  30. }
  31. if ($n > 9)
  32. {
  33. $temp = (string)$n;
  34. $n = (int)$temp[0] + (int)$temp[1];
  35. }
  36. $retorno .= (string)$n;
  37. if (strlen($s) == 1)
  38. {
  39. $retorno .= '9';
  40. }
  41. else
  42. {
  43. if ((int)$s[0] > (int)substr($s, -1, 1))
  44. {
  45. $retorno .= '7';
  46. }
  47. else if ((int)$s[0] < (int)substr($s, -1, 1))
  48. {
  49. $retorno .= '3';
  50. }
  51. else
  52. {
  53. $retorno .= '5';
  54. }
  55.  
  56. }
  57. }
  58. return $retorno;
  59. }
  60.  
  61. private function hasLetters($s)
  62. {
  63. for ($i = 0; $i < strlen($s); $i++)
  64. {
  65. if (ord(substr($s, $i, 1)) < 48 || ord(substr($s, $i, 1)) > 57)
  66. {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }

Report this snippet  

You need to login to post a comment.