| | 254 | /** |
| | 255 | * Checks if a string is a proper decimal format. The format array can be |
| | 256 | * used to specify a decimal length, or a number and decimal length, eg: |
| | 257 | * array(2) would force the number to have 2 decimal places, array(4,2) |
| | 258 | * would force the number to have 4 digits and 2 decimal places. |
| | 259 | * |
| | 260 | * @param string input string |
| | 261 | * @param array decimal format: y or x,y |
| | 262 | * @return boolean |
| | 263 | */ |
| | 264 | public function decimal($str, $format = NULL) |
| | 265 | { |
| | 266 | // Create the pattern |
| | 267 | $pattern = '/^[0-9]%s\.[0-9]%s$/'; |
| | 268 | |
| | 269 | if ( ! empty($format)) |
| | 270 | { |
| | 271 | if (count($format) > 1) |
| | 272 | { |
| | 273 | // Use the format for number and decimal length |
| | 274 | $pattern = sprintf($pattern, '{'.$format[0].'}', '{'.$format[1].'}'); |
| | 275 | } |
| | 276 | elseif (count($format) > 0) |
| | 277 | { |
| | 278 | // Use the format as decimal length |
| | 279 | $pattern = sprintf($pattern, '+', '{'.$format[0].'}'); |
| | 280 | } |
| | 281 | } |
| | 282 | else |
| | 283 | { |
| | 284 | // No format |
| | 285 | $pattern = sprintf($pattern, '+', '+'); |
| | 286 | } |
| | 287 | |
| | 288 | return (bool) preg_match($pattern, (string) $str); |
| | 289 | } |
| | 290 | |