473,241 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 30746
.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bosconian | last post by:
Using preg_replace() is there a simple regexp to strip everything from a string except alpha and numeric chars (a-zA-Z0-9)? $input = "$tring1!"; $pattern = $input = preg_replace($pattern, "",...
1
by: JuniorLinn | last post by:
Hi there - I would like to share this strip of code with our SQL 2000 DBA community. The code below strips all non-numeric characters from a given string field and rebuilds the string. Very...
9
by: Bernie Yaeger | last post by:
I'm trying to control the textbox keypress event to deal with a "." such that it disallows a second "." and no characters after 2 numbers beyond the "." (thus a currency value). I have no problem...
1
by: Dino Buljubasic | last post by:
Hi, Can somebody help me with this problem please. I need to get the value, not just to check for it, of a number pressed but it has to work for both, numbers above QUERTY and numbers entered...
5
by: Reny | last post by:
can any one tell how can i restrict my user to type just numeric character in the textbox.I am using VS.NET 2003 (VB.NET)
8
by: .Net Sports | last post by:
I am checking for text input on a form validation in javascript that required at least one numeric character along with any number of alpha characters for a given input text box. The below is a var...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.