PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/include/utf8/trim.php

https://bitbucket.org/gencer/fluxbb
PHP | 74 lines | 21 code | 9 blank | 44 comment | 3 complexity | e6191912776838947fe156d247d0ae1d 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. * UTF-8 aware replacement for ltrim()
  9. * Note: you only need to use this if you are supplying the charlist
  10. * optional arg and it contains UTF-8 characters. Otherwise ltrim will
  11. * work normally on a UTF-8 string
  12. * @author Andreas Gohr <andi@splitbrain.org>
  13. * @see http://www.php.net/ltrim
  14. * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
  15. * @return string
  16. * @package utf8
  17. * @subpackage strings
  18. */
  19. function utf8_ltrim( $str, $charlist=false)
  20. {
  21. if($charlist === false)
  22. return ltrim($str);
  23. // Quote charlist for use in a characterclass
  24. $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!', '\\\${1}', $charlist);
  25. return preg_replace('/^['.$charlist.']+/u', '', $str);
  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. {
  41. if($charlist === false)
  42. return rtrim($str);
  43. // Quote charlist for use in a characterclass
  44. $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!', '\\\${1}', $charlist);
  45. return preg_replace('/['.$charlist.']+$/u', '', $str);
  46. }
  47. //---------------------------------------------------------------
  48. /**
  49. * UTF-8 aware replacement for trim()
  50. * Note: you only need to use this if you are supplying the charlist
  51. * optional arg and it contains UTF-8 characters. Otherwise trim will
  52. * work normally on a UTF-8 string
  53. * @author Andreas Gohr <andi@splitbrain.org>
  54. * @see http://www.php.net/trim
  55. * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
  56. * @return string
  57. * @package utf8
  58. * @subpackage strings
  59. */
  60. function utf8_trim( $str, $charlist=false)
  61. {
  62. if($charlist === false)
  63. return trim($str);
  64. return utf8_ltrim(utf8_rtrim($str, $charlist), $charlist);
  65. }