/library/Zend/Filter/StringTrim.php

https://bitbucket.org/hamidrezas/melobit · PHP · 124 lines · 46 code · 9 blank · 69 comment · 6 complexity · 97b076fefe2792d187557a0f6fe51354 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: StringTrim.php 24594 2012-01-05 21:27:01Z matthew $
  20. */
  21. /**
  22. * @see Zend_Filter_Interface
  23. */
  24. require_once 'Zend/Filter/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Filter_StringTrim implements Zend_Filter_Interface
  32. {
  33. /**
  34. * List of characters provided to the trim() function
  35. *
  36. * If this is null, then trim() is called with no specific character list,
  37. * and its default behavior will be invoked, trimming whitespace.
  38. *
  39. * @var string|null
  40. */
  41. protected $_charList;
  42. /**
  43. * Sets filter options
  44. *
  45. * @param string|array|Zend_Config $options
  46. * @return void
  47. */
  48. public function __construct($options = null)
  49. {
  50. if ($options instanceof Zend_Config) {
  51. $options = $options->toArray();
  52. } else if (!is_array($options)) {
  53. $options = func_get_args();
  54. $temp['charlist'] = array_shift($options);
  55. $options = $temp;
  56. }
  57. if (array_key_exists('charlist', $options)) {
  58. $this->setCharList($options['charlist']);
  59. }
  60. }
  61. /**
  62. * Returns the charList option
  63. *
  64. * @return string|null
  65. */
  66. public function getCharList()
  67. {
  68. return $this->_charList;
  69. }
  70. /**
  71. * Sets the charList option
  72. *
  73. * @param string|null $charList
  74. * @return Zend_Filter_StringTrim Provides a fluent interface
  75. */
  76. public function setCharList($charList)
  77. {
  78. $this->_charList = $charList;
  79. return $this;
  80. }
  81. /**
  82. * Defined by Zend_Filter_Interface
  83. *
  84. * Returns the string $value with characters stripped from the beginning and end
  85. *
  86. * @param string $value
  87. * @return string
  88. */
  89. public function filter($value)
  90. {
  91. if (null === $this->_charList) {
  92. return $this->_unicodeTrim((string) $value);
  93. } else {
  94. return $this->_unicodeTrim((string) $value, $this->_charList);
  95. }
  96. }
  97. /**
  98. * Unicode aware trim method
  99. * Fixes a PHP problem
  100. *
  101. * @param string $value
  102. * @param string $charlist
  103. * @return string
  104. */
  105. protected function _unicodeTrim($value, $charlist = '\\\\s')
  106. {
  107. $chars = preg_replace(
  108. array( '/[\^\-\]\\\]/S', '/\\\{4}/S', '/\//'),
  109. array( '\\\\\\0', '\\', '\/' ),
  110. $charlist
  111. );
  112. $pattern = '^[' . $chars . ']*|[' . $chars . ']*$';
  113. return preg_replace("/$pattern/sSD", '', $value);
  114. }
  115. }