number not numeric issue | Member | | Join Date: Oct 2007
Posts: 110
| |
I've got a value I'm grabbing a price range through preg-match from an external page.
I take the value, and then split it into two pieces with explode, and then I split the value again using with list. (code below).
When I look at the output, it looks like both values are numbers, but running is_numeric returns only the 2nd number as being a number.
I've tried using settype, but no luck.
How can i get this number to work? -
if(strstr($showData[3], "$")){
-
$prices=explode("</td><td>", $showData[3]);
-
list($low, $high)=split('-', $prices[0]);
-
-
-
$low=addslashes(str_replace('$', '', $low));
-
$high=addslashes(str_replace('$', '', $high));
-
$low=addslashes(trim($low));
-
$high=addslashes(trim($high));
-
-
if(!is_numeric($low)){
-
echo "low price is not a number<br/>";
-
settype($low, "integer");
-
}
-
if(!is_numeric($high)){
-
echo "high price is not a number<br/>";
-
}
-
echo "$low, $high <br/>";
-
The really strange part is that the second value is returned as a number, its only the first one that isn't.
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | re: number not numeric issue
There's probably a space there. The best way to do debug on your variables isn't to put them in an if() switch, try print_r() or better yet var_dump().
PHP is loosely typed, why do you specifically need to convert it to an integer? If you were to put this value in a mathmatical expression PHP would automatically convert it so that string(" 92") becomes integer(92).
you can also use intval() or type cast it as
$intVal = (int) $strVal;
Good luck,
Dan
| | Member | | Join Date: Oct 2007
Posts: 110
| | | re: number not numeric issue
Thanks Dan,
The problem is that for some reason this value which prints fine, won't go into the database. When I print the mysql statement, it all looks fine. When I check how the values went into the database, only the $high value makes it in. the $low is always 0.
When I copy the printed mysql from my browser and put it into mysql directly, then that value goes in.
There are no spaces (and I've added an extra str_replace to remove any if they existed), and I also tried the (int)$low, but nothing seems to be working.
I find it very strange that the is_numeric is triggered for $low and not $high as they each go through the same process.
However, the root of the problem is that I can't insert the $low value into mysql from php, but can by copying the php output of the sql query and putting it in directly.
Any idea what would cause this?
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | re: number not numeric issue
only an invalid value causes a 0 in MySQL.
Did you try the die(var_dump($high) . " " . var_dump($low)); debug print line I told you? Quote:
Originally Posted by pedalpete Thanks Dan,
The problem is that for some reason this value which prints fine, won't go into the database. When I print the mysql statement, it all looks fine. When I check how the values went into the database, only the $high value makes it in. the $low is always 0.
When I copy the printed mysql from my browser and put it into mysql directly, then that value goes in.
There are no spaces (and I've added an extra str_replace to remove any if they existed), and I also tried the (int)$low, but nothing seems to be working.
I find it very strange that the is_numeric is triggered for $low and not $high as they each go through the same process.
However, the root of the problem is that I can't insert the $low value into mysql from php, but can by copying the php output of the sql query and putting it in directly.
Any idea what would cause this? |  | Expert | | Join Date: Feb 2008 Location: Australia
Posts: 914
| | | re: number not numeric issue
Can you just add and echo after line 6 and 7 so we can see what is stored as $low and $high.
***possibly exposing ignorance of the list() function but shouldn't line three be $prices instead of $prices[0]?
| | Member | | Join Date: Oct 2007
Posts: 110
| | | re: number not numeric issue
I have now tried the var_dump, it returns a string,
string(6) "55"
for where the string is 55.
I guess the strange part is why it thinks the string is 6 characters when it then prints it out as 2 characters?
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,751
| | | re: number not numeric issue
Are you copying that from the source or just from the browser window?
If you are doing the latter, don't. The browsers renders away things like white-spaces, so you may not be seeing the extra characters if they are there.
Do "View Source" (or whatever flavor of that your browser uses) and take a look at it there.
In your code, you run both strings through the addslashes function twice before trying to use them... why?
It seems an odd thing to do to numeric strings.
| | Member | | Join Date: Oct 2007
Posts: 110
| | | re: number not numeric issue
Thanks once again for coming to the rescue.
Of course when I go look at it in the source, their is an html tag still in the $low variable.
did str_replace on that , and it all works.
Thanks
Pete
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|