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 &...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 > > > |
|
|||
|
.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 |
|
|||
|
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 |
|
|||
|
> 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 |
|
|||
|
> "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 |
|
|||
|
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 |
|
|||
|
.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 |
|
|||
|
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??? > |