is there a quick way to remove non numeric chars

This is a discussion on is there a quick way to remove non numeric chars within the PHP Language forums, part of the PHP Programming Forums category; try this <? $str="222223433934"; if (ctype_digit($str)) { print "The string is all digits"; } else { print &...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-30-2004
Peter
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

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" <netizen@gmx.net> wrote in message
> news:t66pq013drdjjfteq93qsnt2ve2683gel7@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

>
>
>

Reply With Quote
  #2 (permalink)  
Old 11-30-2004
chris
 
Posts: n/a
Default 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???


Reply With Quote
  #3 (permalink)  
Old 11-30-2004
Michael Fesser
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

.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
Reply With Quote
  #4 (permalink)  
Old 11-30-2004
chris
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

that didnt seem to work But it gives me something to go on with

thanks

"Michael Fesser" <netizen@gmx.net> wrote in message
news:t66pq013drdjjfteq93qsnt2ve2683gel7@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



Reply With Quote
  #5 (permalink)  
Old 11-30-2004
Hilarion
 
Posts: n/a
Default Re: 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???


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


Reply With Quote
  #6 (permalink)  
Old 11-30-2004
Daedalus
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

> $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()."


Reply With Quote
  #7 (permalink)  
Old 11-30-2004
Hilarion
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

> "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


Reply With Quote
  #8 (permalink)  
Old 11-30-2004
Kelv
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

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

Reply With Quote
  #9 (permalink)  
Old 12-01-2004
Michael Fesser
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

.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
Reply With Quote
  #10 (permalink)  
Old 12-02-2004
d
 
Posts: n/a
Default Re: is there a quick way to remove non numeric chars

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" <someone@here.com> wrote in message
news:41ac97c6$1@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???
>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:06 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0