PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/utf8/substr.php

https://bitbucket.org/alvinpd/monsterninja
PHP | 72 lines | 45 code | 8 blank | 19 comment | 10 complexity | 192bae535473ccb026ca66355e17b85a MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * UTF8::substr
  4. *
  5. * @package Kohana
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2008 Kohana Team
  8. * @copyright (c) 2005 Harry Fuecks
  9. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  10. */
  11. function _substr($str, $offset, $length = NULL)
  12. {
  13. if (UTF8::is_ascii($str))
  14. return ($length === NULL) ? substr($str, $offset) : substr($str, $offset, $length);
  15. // Normalize params
  16. $str = (string) $str;
  17. $strlen = UTF8::strlen($str);
  18. $offset = (int) ($offset < 0) ? max(0, $strlen + $offset) : $offset; // Normalize to positive offset
  19. $length = ($length === NULL) ? NULL : (int) $length;
  20. // Impossible
  21. if ($length === 0 OR $offset >= $strlen OR ($length < 0 AND $length <= $offset - $strlen))
  22. return '';
  23. // Whole string
  24. if ($offset == 0 AND ($length === NULL OR $length >= $strlen))
  25. return $str;
  26. // Build regex
  27. $regex = '^';
  28. // Create an offset expression
  29. if ($offset > 0)
  30. {
  31. // PCRE repeating quantifiers must be less than 65536, so repeat when necessary
  32. $x = (int) ($offset / 65535);
  33. $y = (int) ($offset % 65535);
  34. $regex .= ($x == 0) ? '' : '(?:.{65535}){'.$x.'}';
  35. $regex .= ($y == 0) ? '' : '.{'.$y.'}';
  36. }
  37. // Create a length expression
  38. if ($length === NULL)
  39. {
  40. $regex .= '(.*)'; // No length set, grab it all
  41. }
  42. // Find length from the left (positive length)
  43. elseif ($length > 0)
  44. {
  45. // Reduce length so that it can't go beyond the end of the string
  46. $length = min($strlen - $offset, $length);
  47. $x = (int) ($length / 65535);
  48. $y = (int) ($length % 65535);
  49. $regex .= '(';
  50. $regex .= ($x == 0) ? '' : '(?:.{65535}){'.$x.'}';
  51. $regex .= '.{'.$y.'})';
  52. }
  53. // Find length from the right (negative length)
  54. else
  55. {
  56. $x = (int) (-$length / 65535);
  57. $y = (int) (-$length % 65535);
  58. $regex .= '(.*)';
  59. $regex .= ($x == 0) ? '' : '(?:.{65535}){'.$x.'}';
  60. $regex .= '.{'.$y.'}';
  61. }
  62. preg_match('/'.$regex.'/us', $str, $matches);
  63. return $matches[1];
  64. }