Source of file StringTrim.php
Size: 2,579 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Filter/StringTrim.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Filter; use Traversable; class StringTrim extends AbstractFilter { /** * @var array */ protected $options = array( 'charlist' => null, ); /** * Sets filter options * * @param string|array|Traversable $charlistOrOptions */ public function __construct($charlistOrOptions = null) { if ($charlistOrOptions !== null) { if (!is_array($charlistOrOptions) && !$charlistOrOptions instanceof Traversable) { $this->setCharList($charlistOrOptions); } else { $this->setOptions($charlistOrOptions); } } } /** * Sets the charList option * * @param string $charList * @return self Provides a fluent interface */ public function setCharList($charList) { if (empty($charList)) { $charList = null; } $this->options['charlist'] = $charList; return $this; } /** * Returns the charList option * * @return string|null */ public function getCharList() { return $this->options['charlist']; } /** * Defined by Zend\Filter\FilterInterface * * Returns the string $value with characters stripped from the beginning and end * * @param string $value * @return string */ public function filter($value) { if (!is_string($value)) { return $value; } $value = (string) $value; if (null === $this->options['charlist']) { return $this->unicodeTrim($value); } return $this->unicodeTrim($value, $this->options['charlist']); } /** * Unicode aware trim method * Fixes a PHP problem * * @param string $value * @param string $charlist * @return string */ protected function unicodeTrim($value, $charlist = '\\\\s') { $chars = preg_replace( array('/[\^\-\]\\\]/S', '/\\\{4}/S', '/\//'), array('\\\\\\0', '\\', '\/'), $charlist ); $pattern = '/^[' . $chars . ']+|[' . $chars . ']+$/usSD'; return preg_replace($pattern, '', $value); } } |