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.
16 * @package Zend_Filter
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Filter_Interface
25 require_once 'Zend/Filter/Interface.php';
29 require_once 'Zend/Locale.php';
33 * @package Zend_Filter
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 class Zend_Filter_Alpha
implements Zend_Filter_Interface
40 * Whether to allow white space characters; off by default
45 public $allowWhiteSpace;
48 * Is PCRE is compiled with UTF-8 and Unicode support
52 protected static $_unicodeEnabled;
57 * @var Zend_Locale object
62 * The Alphabet means english alphabet.
66 protected static $_meansEnglishAlphabet;
69 * Sets default option values for this instance
71 * @param boolean $allowWhiteSpace
74 public function __construct($allowWhiteSpace = false)
76 if ($allowWhiteSpace instanceof Zend_Config
) {
77 $allowWhiteSpace = $allowWhiteSpace->toArray();
78 } else if (is_array($allowWhiteSpace)) {
79 if (array_key_exists('allowwhitespace', $allowWhiteSpace)) {
80 $allowWhiteSpace = $allowWhiteSpace['allowwhitespace'];
82 $allowWhiteSpace = false;
86 $this->allowWhiteSpace
= (boolean
) $allowWhiteSpace;
87 if (null === self
::$_unicodeEnabled) {
88 self
::$_unicodeEnabled = (@preg_match
('/\pL/u', 'a')) ?
true : false;
91 if (null === self
::$_meansEnglishAlphabet) {
92 $this->_locale
= new Zend_Locale('auto');
93 self
::$_meansEnglishAlphabet = in_array($this->_locale
->getLanguage(),
94 array('ja', 'ko', 'zh')
101 * Returns the allowWhiteSpace option
105 public function getAllowWhiteSpace()
107 return $this->allowWhiteSpace
;
111 * Sets the allowWhiteSpace option
113 * @param boolean $allowWhiteSpace
114 * @return Zend_Filter_Alpha Provides a fluent interface
116 public function setAllowWhiteSpace($allowWhiteSpace)
118 $this->allowWhiteSpace
= (boolean
) $allowWhiteSpace;
123 * Defined by Zend_Filter_Interface
125 * Returns the string $value, removing all but alphabetic characters
127 * @param string $value
130 public function filter($value)
132 $whiteSpace = $this->allowWhiteSpace ?
'\s' : '';
133 if (!self
::$_unicodeEnabled) {
134 // POSIX named classes are not supported, use alternative a-zA-Z match
135 $pattern = '/[^a-zA-Z' . $whiteSpace . ']/';
136 } else if (self
::$_meansEnglishAlphabet) {
137 //The Alphabet means english alphabet.
138 $pattern = '/[^a-zA-Z' . $whiteSpace . ']/u';
140 //The Alphabet means each language's alphabet.
141 $pattern = '/[^\p{L}' . $whiteSpace . ']/u';
144 return preg_replace($pattern, '', (string) $value);