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-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id: Alpha.php 16870 2009-07-20 10:17:59Z mikaelkael $
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-2009 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 $this->allowWhiteSpace
= (boolean
) $allowWhiteSpace;
77 if (null === self
::$_unicodeEnabled) {
78 self
::$_unicodeEnabled = (@preg_match
('/\pL/u', 'a')) ?
true : false;
81 if (null === self
::$_meansEnglishAlphabet) {
82 $this->_locale
= new Zend_Locale('auto');
83 self
::$_meansEnglishAlphabet = in_array($this->_locale
->getLanguage(),
84 array('ja', 'ko', 'zh')
91 * Returns the allowWhiteSpace option
95 public function getAllowWhiteSpace()
97 return $this->allowWhiteSpace
;
101 * Sets the allowWhiteSpace option
103 * @param boolean $allowWhiteSpace
104 * @return Zend_Filter_Alpha Provides a fluent interface
106 public function setAllowWhiteSpace($allowWhiteSpace)
108 $this->allowWhiteSpace
= (boolean
) $allowWhiteSpace;
113 * Defined by Zend_Filter_Interface
115 * Returns the string $value, removing all but alphabetic characters
117 * @param string $value
120 public function filter($value)
122 $whiteSpace = $this->allowWhiteSpace ?
'\s' : '';
123 if (!self
::$_unicodeEnabled) {
124 // POSIX named classes are not supported, use alternative a-zA-Z match
125 $pattern = '/[^a-zA-Z' . $whiteSpace . ']/';
126 } else if (self
::$_meansEnglishAlphabet) {
127 //The Alphabet means english alphabet.
128 $pattern = '/[^a-zA-Z' . $whiteSpace . ']/u';
130 //The Alphabet means each language's alphabet.
131 $pattern = '/[^\p{L}' . $whiteSpace . ']/u';
134 return preg_replace($pattern, '', (string) $value);