...

View Full Version : How do you allow only numeric characters in a string? ++ more..



cyphix
05-11-2004, 04:39 PM
Ok...

I know how you can make stuff lowercase like this..



if (strtolower($var) == strtolower($var)) {


But how can you allow only numeric values in a string? Meaning if it comes back with a hypen or whitespace, brackets etc etc it will strip them out & leave ONLY numeric characters just like strtolower makes everything lowercase?

Also, another quick question..

How can you check if a string contains a certain character? So if I wanted to check that they put in a "@" I could check that the string contains one.

Cheers!

cpradio
05-11-2004, 05:28 PM
is_numeric() (http://us4.php.net/manual/en/function.is-numeric.php)

cyphix
05-11-2004, 07:38 PM
That isn't going to work how I need it to work according to what I have read on that page.

From the user comments something like this would seem to work better..



eregi("[^0-9]",$var)


But I wouldn't have a clue what that means or how it works; I just know what it does lol

litebearer
05-11-2004, 07:42 PM
I use this function for money variables, but you could use it for your purposes




function numpass_filter($userString){
$money = explode(".", $userString);
//next remove all characters save 0 though 9
//in both elements of the array
$dollars = eregi_replace("[^0-9]", null, $money[0]);
$cents = eregi_replace("[^0-9]", null, $money[1]);

//if there was a decimal in the original string, put it back
if((string)$cents!=null){
$cents = "." . $cents;
}
$result = $dollars . $cents;
return($result);
}

cpradio
05-11-2004, 08:23 PM
That isn't going to work how I need it to work according to what I have read on that page.

From the user comments something like this would seem to work better..



eregi("[^0-9]",$var)


But I wouldn't have a clue what that means or how it works; I just know what it does lol


Maybe I missed something, but could you tell me why is_numeric will not work for you? All it does is ensure the string you send it is all numbers....that is what you wanted right? Decimals are allowed...

As for checking for a certain character:

if (strpos($input, "@") !== false) {
// @ symbol was found
} else {
// @ symbol was not found
}

You use !== versus != because it returns the location in the string of where the character was found, and if the first character is the '@' symbol then it would return a 0.

-Matt

litebearer
05-11-2004, 08:29 PM
I believe he wanted the ability to strip out all non-numeric characters.


Meaning if it comes back with a hypen or whitespace, brackets etc etc it will strip them out & leave ONLY numeric characters just like strtolower makes everything lowercase?


Lite...

bcarl314
05-11-2004, 08:38 PM
How about...



$string = "943856.2523";
if(preg_match("/[^0-9\.]/",$string)) {
print "this is not just numbers";
}
else {
print "only numbers";
}

cpradio
05-11-2004, 08:44 PM
I believe he wanted the ability to strip out all non-numeric characters.


Lite...

Ah, so I did miss it. Sorry.

cyphix
05-11-2004, 08:46 PM
Lite is correct cpradio.. I can't allow decimals.

Thanks for the code to check for certain characters too.

Lite.. thanks for sharing your code, but I'm not all sure how to edit it to work for me .. I've only been learning PHP for about a week now & some of that stuff is new to me.

I had a look around php.net & came up with this, hope it works..

It doesn't get rid of all non-numeric characters but replaces the more common chars people would input into phone fields.



// Sets $ph to $phone so I can refer to $phone later & get the initial value
$ph = $phone;

$ph_pattern[0] = "(";
$ph_pattern[1] = ")";
$ph_pattern[2] = "-";
$ph_pattern[3] = ".";
$ph_pattern[4] = " ";

$ph_rep[4] = "";
$ph_rep[3] = "";
$ph_rep[2] = "";
$ph_rep[1] = "";
$ph_rep[0] = "";

preg_replace($ph_pattern, $ph_rep, $ph);

$sp_pattern[0] = "(";
$sp_pattern[1] = ")";
$sp_pattern[2] = "-";
$sp_pattern[3] = ".";
$sp_pattern[4] = " ";

$sp_rep[4] = "";
$sp_rep[3] = "";
$sp_rep[2] = "";
$sp_rep[1] = "";
$sp_rep[0] = "";

preg_replace($sp_pattern, $sp_rep, $searchphone);

bcarl314
05-11-2004, 09:01 PM
um so what you want is...



$str = "(555) 123-4567";
$clean = preg_replace("/[^0-9]/","",$str);


In fact, if you're talking about phone numbers (in the us) they must be 3+3+4 = 10 or 1+3+3+4 = 11, so you could even do this...



$str = "(555) 123-4567";
$clean = preg_replace("/[^0-9]/","",$str);
if(preg_match("/[0-9]{10,11}/",$clean)) {
print "Good";
}
else {
print "Bad";
}

cyphix
05-11-2004, 09:05 PM
Thanks bcarl.. I'll try that. :D

My code was getting errors ayway..

Warning: No ending matching delimiter ')' found in xxxxxxxxxxxxx/search.php on line 158

Warning: No ending delimiter ')' found in xxxxxxxxxxxxx/search.php on line 158

Warning: No ending delimiter '-' found in xxxxxxxxxxxxx/search.php on line 158

Warning: No ending delimiter '.' found in xxxxxxxxxxxxx/search.php on line 158

Warning: Empty regular expression in xxxxxxxxxxxxx/search.php on line 158

Warning: No ending delimiter '$' found in xxxxxxxxxxxxx/search.php on line 172

cyphix
05-11-2004, 09:15 PM
Worked good! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum