473,320 Members | 1,831 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,320 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 30750
.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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.