469,268 Members | 962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

is there a quick way to remove non numeric chars

is there a quick and easy way to check a string for non numeric characters ?

i have thought of making a routine to check each character to confirm it is
0 - 9 but was wondering if there was a quicker way???
Jul 17 '05 #1
9 30580
.oO(chris)
is there a quick and easy way to check a string for non numeric characters ?

i have thought of making a routine to check each character to confirm it is
0 - 9 but was wondering if there was a quicker way???


For example regular expressions:

$isNonNumeric = preg_match('#[^\d]#', $yourString);

Micha
Jul 17 '05 #2
that didnt seem to work But it gives me something to go on with

thanks

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:t6********************************@4ax.com...
.oO(chris)
is there a quick and easy way to check a string for non numeric characters
?

i have thought of making a routine to check each character to confirm it
is
0 - 9 but was wondering if there was a quicker way???


For example regular expressions:

$isNonNumeric = preg_match('#[^\d]#', $yourString);

Micha

Jul 17 '05 #3
> is there a quick and easy way to check a string for non numeric characters ?

i have thought of making a routine to check each character to confirm it is 0 - 9 but was wondering if there was a quicker way???


Do you want to only check if there are non-numeric chars (as stated in message
body), or remove them (as stated in message subject)?
If only check, then use "is_numeric" function. If replace, then you can
use some regular expressions replacement like:

$result = ereg_replace( '[^0-9]+', '', $source );
Hilarion
Jul 17 '05 #4
> $result = ereg_replace( '[^0-9]+', '', $source );


From the manual page:
"Note: preg_replace(), which uses a Perl-compatible regular expression
syntax, is often a faster alternative to ereg_replace()."
Jul 17 '05 #5
try this

<?
$str="222223433934";

if (ctype_digit($str))
{
print "The string is all digits";
}
else
{
print "the string has one or more characters that are not digits";
}
?>
chris wrote:
that didnt seem to work But it gives me something to go on with

thanks

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:t6********************************@4ax.com...
.oO(chris)

is there a quick and easy way to check a string for non numeric characters
?

i have thought of making a routine to check each character to confirm it
is
0 - 9 but was wondering if there was a quicker way???


For example regular expressions:

$isNonNumeric = preg_match('#[^\d]#', $yourString);

Micha


Jul 17 '05 #6
> "Note: preg_replace(), which uses a Perl-compatible regular expression
syntax, is often a faster alternative to ereg_replace()."


That's true (but irrelevant if we are talking about user input validation).
And AFAIK preg functions are binary safe, where ereg functions are not
(which may be much more important).

But I do not know Perl regular expressions syntax, and personaly think that
posix syntax is easier to read.

Hilarion
Jul 17 '05 #7
chris wrote:
is there a quick and easy way to check a string for non numeric characters ?

i have thought of making a routine to check each character to confirm it is
0 - 9 but was wondering if there was a quicker way???


preg_match("%[^0-9]%",$string);

If there's anything other than 0-9 in $string, the condition returns true.

Kelv :)

--
LoHost
http://www.lohost.com

Jul 17 '05 #8
.oO(Hilarion)
But I do not know Perl regular expressions syntax, and personaly think that
posix syntax is easier to read.


The basic syntax is the same, except for the delimiters, which are
necessary in PCRE. Anything else are extensions, which make PCRE much
more flexible and powerful.

Micha
Jul 17 '05 #9
d
No-one reads the PHP manual here it seems ;) There is a built-in function
for doing this:

if (ctype_digit($string)) {
// $string contains only numbers, no other characters
}

"chris" <so*****@here.com> wrote in message
news:41********@funnel.arach.net.au...
is there a quick and easy way to check a string for non numeric characters
?

i have thought of making a routine to check each character to confirm it
is 0 - 9 but was wondering if there was a quicker way???

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bosconian | last post: by
9 posts views Thread by Bernie Yaeger | last post: by
10 posts views Thread by Mike Copeland | last post: by
26 posts views Thread by Brad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.