PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/include/utf8/trim.php

https://bitbucket.org/gencer/punbb
PHP | 78 lines | 22 code | 9 blank | 47 comment | 3 complexity | 4873dc555ed5aa87c931213eb4a0f5ed MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @version $Id: trim.php,v 1.1 2006/02/25 13:50:17 harryf Exp $
  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) {
  22. return ltrim($str);
  23. }
  24. // Quote charlist for use in a characterclass
  25. $charlist = preg_quote($charlist, '#');
  26. return preg_replace('#^['.$charlist.']+#u', '', $str);
  27. }
  28. //---------------------------------------------------------------
  29. /**
  30. * UTF-8 aware replacement for rtrim()
  31. * Note: you only need to use this if you are supplying the charlist
  32. * optional arg and it contains UTF-8 characters. Otherwise rtrim will
  33. * work normally on a UTF-8 string
  34. * @author Andreas Gohr <andi@splitbrain.org>
  35. * @see http://www.php.net/rtrim
  36. * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
  37. * @return string
  38. * @package utf8
  39. * @subpackage strings
  40. */
  41. function utf8_rtrim( $str, $charlist = FALSE ) {
  42. if ($charlist === FALSE) {
  43. return rtrim($str);
  44. }
  45. // Quote charlist for use in a characterclass
  46. $charlist = preg_quote($charlist, '#');
  47. return preg_replace('#['.$charlist.']+$#u', '', $str);
  48. }
  49. //---------------------------------------------------------------
  50. /**
  51. * UTF-8 aware replacement for trim()
  52. * Note: you only need to use this if you are supplying the charlist
  53. * optional arg and it contains UTF-8 characters. Otherwise trim will
  54. * work normally on a UTF-8 string
  55. * @author Andreas Gohr <andi@splitbrain.org>
  56. * @see http://www.php.net/trim
  57. * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
  58. * @return string
  59. * @package utf8
  60. * @subpackage strings
  61. */
  62. function utf8_trim( $str, $charlist = FALSE ) {
  63. if ($charlist === FALSE) {
  64. return trim($str);
  65. }
  66. // Quote charlist for use in a characterclass
  67. $charlist = preg_quote($charlist, '#');
  68. return preg_replace('#^['.$charlist.']+|['.$charlist.']+$#u', '', $str);
  69. }