Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

modifier.is_numeric (new)

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
xe
Smarty Rookie


Joined: 25 Sep 2003
Posts: 8

PostPosted: Thu Sep 25, 2003 9:24 am    Post subject: modifier.is_numeric (new) Reply with quote

Hi.

Maybe this is useful for anyone. Should explain itself.

[php:1:34c926a591]
<?php
/*
* Smarty {is_number} function plugin
*
* Type: function
* Name: is_value
* Purpose: Simple modifier which tries to finds out if a given value is a number.
* Handles "." and "," and whitespaces often found in untyped variables.
* Author: Matthias H. Risse <php at nonospam risse dot org>
*/

/* Usage:
* {if $element|is_number}
* <div align="right">
* {$element|default:"&"}
* </div>
* {else}
* {$element|default:"&"}
* {/if}
*/

function smarty_modifier_is_number($params)
{
if(preg_match ("/^([0-9]+)$/", $params))
{
$ret = 1;
} else {
$params = str_replace('.','',$params);
$params = str_replace(',','',$params);
$params = str_replace('-','',$params);
$params = str_replace(' ','',$params);
if(preg_match ("/^([0-9]+)$/", $params))
{
$ret = 1;
} else {
$ret = 0;
}
}
return $ret;
}
?>[/php:1:34c926a591] Twisted Evil Twisted Evil Twisted Evil


Last edited by xe on Thu Sep 25, 2003 1:03 pm; edited 4 times in total
Back to top
View user's profile Send private message
joscha
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Metzingen, Germany

PostPosted: Thu Sep 25, 2003 12:25 pm    Post subject: Reply with quote

Why don't you use the built-in is_numeric() function of PHP? Any special reason to preg_* the passed param?

greets,
Joscha
Back to top
View user's profile Send private message Visit poster's website
xe
Smarty Rookie


Joined: 25 Sep 2003
Posts: 8

PostPosted: Thu Sep 25, 2003 1:00 pm    Post subject: Reply with quote

i tried that, but the results dont match the needs.

this example should make it clearer:

[php:1:293426569e]
echo is_numeric('005555111'); // results true
echo "<br><br>";
echo is_numeric('00CCCC111'); // results true, too.
[/php:1:293426569e]

this simply happends, because is_numeric accepts "numeric strings" and (see php manual) and thats not what we want it to do.

probably numeric strings are defined upon the ascii/ansi codepage (we have a number for each character we use) and thats the reason

[php:1:293426569e]
is_numeric('1AABB02');
[/php:1:293426569e]

considers alphanumeric characters (and others) to be valid "numeric" values.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Sep 25, 2003 1:47 pm    Post subject: Reply with quote

xe: i see that your modifier matches more than is_numeric() (whitespaces and "," or ".") and so has it's use.

but is_numeric('00CCCC111') and is_numeric('1AABB02') return false in my tests. is_numeric() returns true for "numeric strings". but this does not mean alphanumeric strings. maybe you mean things like "0x1AABB02" which is a numeric (hexadecimal) string
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Sep 25, 2003 9:09 pm    Post subject: Reply with quote

In my mind, is_numeric() has some very presecribed functionality and other than that, you have to roll your own. It appears that there are several built-in PHP in functions like that.

Messju is correct about xe's examples, but is_numeric still has some potential gothas to consider:

* is_numeric("1e2") => true; exponent recognized
* is_numeric("1,2") => true/false; depends on current locale settings
* is_numeric(" 123.123") => true; leading whitespace ignored
* is_numeric("123.123 ") => false; trailing whitespace not ignored
Back to top
View user's profile Send private message
xe
Smarty Rookie


Joined: 25 Sep 2003
Posts: 8

PostPosted: Sat Sep 27, 2003 9:54 am    Post subject: Reply with quote

jeah.

after my last post i just realized that is_numeric() could recognize hex.

howeverm i think the point is, that you sometimes need a function for quick formating of a untyped variable and that is what this baby here is supposed to do. if you have some optimization, please feel free to update the code.

xe

ps: of course php's string-modifiers such as trim() and co. might be useful.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP