PDA

View Full Version : str_replace coding help


lansing
06-04-2006, 12:44 AM
I have an input box that needs to be nothing but numbers 1,2,3,4,5,6,7,8,9,0 & nothing else except for a single . period!! This input box is for currency amount...I don't think anybody is going to enter any other characters other than numbers & a period, but I know somebody will & it will mess up my inputs.

I am using JavaScript as the first way to make sure the format is the way I want it, but I need a way of checking using php for my user's that have JavaScript disable. I will post my JavaScript code below in case you need to see it.

The reason I MUST get everything but numbers & a single period out is because I am using these inputs to do some simple math before inserting in the DB.

This is the php code I have got so farr, but this is very very basic & not enough to do what I need done. Is there some script that I could use that would strip EVERY CHARACTER, but numbers & a single period?$strip_this = array("$", ",");
$striped_data = str_replace($strip_this, "", "$amount");



My JavaScript code function validate(field) {
var valid = ".0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid format. Do not enter dollar signs or commas!");
field.focus();
//field.select();
}
}


Thanks!

lavinpj1
06-04-2006, 01:03 AM
something like...

<?php

function checkBadChars($str) {
$goodList = array("1","2","3","4","5","6","7","8","9","0","."); //Set good characters to array
for ($i = 0; $i < strlen($str); $i++) { //loop through inputted string
if (!in_array($str[$i], $goodList)) { //check if it's a bad character
return FALSE; //if so, return false
}
if ($str[$i] == ".") { //check if it's a .
if (isset($hadDot)) { //check if we have already had a .
return FALSE; //if so, return false
}
$hadDot = 1; //if not, set the variable so we know we have had a .
}
}
return TRUE; //if we got to the end with no trouble, return true
}

?>

Then, you would just do something like...

<?php
$myStr = "123456hy0.";
if (checkBadChars($myStr)) {
echo "The string is just fine!";
}
else {
echo "The string is bad!";
}
?>


~Phil~

lavinpj1
06-04-2006, 01:31 AM
Actually, the above was wrong, due to PHP sucking! I have edited the above, making each item in the array a string in order for it to work.

~Phil~

ralph l mayo
06-04-2006, 02:07 AM
It's probably better to just match whether the input is valid rather than trying to force it to be valid with replacements. It's difficult to deal with ambiguous cases like 1.203... Typoed 3 in $1.20 or is the dot a thousand delimiter? You can use this to check if a string is only numbers followed by an optional dot with exactly two numbers following it, or no leading numbers with a mandatory dot and two numbers, ie. .12 is ok, 1234 is ok, 123.45 is ok, 123.4 isn't, 1.234 isn't, etc.


if (!preg_match('/^(?:\d+(?:\.\d{2})?|^\.\d{2})$/', $amount))
{
die('Please input currency in the form 123.45, without other symbols.');
}
else
{
// Looks good, carry on.
}

marek_mar
06-04-2006, 11:53 AM
is_numeric() (http://www.php.net/is_numeric)?

<?php
if (!is_numeric($amount))
{
die('Please input currency in the form 123.45, without other symbols.');
}
?>

ralph l mayo
06-04-2006, 06:34 PM
is_numeric is numeric allows things like 0.1234, -12, 1.23E10 (possibly valid, but not how you'd want your input), and 0xFFFFF.

marek_mar
06-05-2006, 11:20 AM
It is still a number. And base 16 input won't work as it doesn't convert from string.

Base 16 can actually convert to int/float correctly but only when you do a mathematical operationn on it and not when you change it's type which is odd becouse strings are changed to int/float by PHP when you try to do mathematical operations on them.