3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Filter
;
12 use Zend\Stdlib\StringUtils
;
14 class Digits
extends AbstractFilter
17 * Defined by Zend\Filter\FilterInterface
19 * Returns the string $value, removing all but digit characters
21 * If the value provided is non-scalar, the value will remain unfiltered
22 * and an E_USER_WARNING will be raised indicating it's unfilterable.
24 * @param string $value
25 * @return string|mixed
27 public function filter($value)
29 if (null === $value) {
33 if (!is_scalar($value)) {
36 '%s expects parameter to be scalar, "%s" given; cannot filter',
38 (is_object($value) ?
get_class($value) : gettype($value))
45 if (!StringUtils
::hasPcreUnicodeSupport()) {
46 // POSIX named classes are not supported, use alternative 0-9 match
47 $pattern = '/[^0-9]/';
48 } elseif (extension_loaded('mbstring')) {
49 // Filter for the value with mbstring
50 $pattern = '/[^[:digit:]]/';
52 // Filter for the value without mbstring
53 $pattern = '/[\p{^N}]/';
56 return preg_replace($pattern, '', (string) $value);