PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/phputf8/trim.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 68 lines | 15 code | 7 blank | 46 comment | 3 complexity | 35ac4732e647d596a038d47daeb7b9eb MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package utf8
  5. * @subpackage strings
  6. */
  7. //---------------------------------------------------------------
  8. /**
  9. * UTF-8 aware replacement for ltrim()
  10. * Note: you only need to use this if you are supplying the charlist
  11. * optional arg and it contains UTF-8 characters. Otherwise ltrim will
  12. * work normally on a UTF-8 string
  13. * @author Andreas Gohr <andi@splitbrain.org>
  14. * @see http://www.php.net/ltrim
  15. * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
  16. * @return string
  17. * @package utf8
  18. * @subpackage strings
  19. */
  20. function utf8_ltrim( $str, $charlist = FALSE ) {
  21. if($charlist === FALSE) return ltrim($str);
  22. //quote charlist for use in a characterclass
  23. $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!','\\\${1}',$charlist);
  24. return preg_replace('/^['.$charlist.']+/u','',$str);
  25. }
  26. //---------------------------------------------------------------
  27. /**
  28. * UTF-8 aware replacement for rtrim()
  29. * Note: you only need to use this if you are supplying the charlist
  30. * optional arg and it contains UTF-8 characters. Otherwise rtrim will
  31. * work normally on a UTF-8 string
  32. * @author Andreas Gohr <andi@splitbrain.org>
  33. * @see http://www.php.net/rtrim
  34. * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
  35. * @return string
  36. * @package utf8
  37. * @subpackage strings
  38. */
  39. function utf8_rtrim( $str, $charlist = FALSE ) {
  40. if($charlist === FALSE) return rtrim($str);
  41. //quote charlist for use in a characterclass
  42. $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!','\\\${1}',$charlist);
  43. return preg_replace('/['.$charlist.']+$/u','',$str);
  44. }
  45. //---------------------------------------------------------------
  46. /**
  47. * UTF-8 aware replacement for trim()
  48. * Note: you only need to use this if you are supplying the charlist
  49. * optional arg and it contains UTF-8 characters. Otherwise trim will
  50. * work normally on a UTF-8 string
  51. * @author Andreas Gohr <andi@splitbrain.org>
  52. * @see http://www.php.net/trim
  53. * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
  54. * @return string
  55. * @package utf8
  56. * @subpackage strings
  57. */
  58. function utf8_trim( $str, $charlist = FALSE ) {
  59. if($charlist === FALSE) return trim($str);
  60. return utf8_ltrim(utf8_rtrim($str, $charlist), $charlist);
  61. }