473,395 Members | 1,666 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,395 software developers and data experts.

number not numeric issue

110 100+
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?

Expand|Select|Wrap|Line Numbers
  1. if(strstr($showData[3], "$")){
  2.         $prices=explode("</td><td>", $showData[3]);
  3.         list($low, $high)=split('-', $prices[0]);
  4.  
  5.  
  6.         $low=addslashes(str_replace('$', '', $low));
  7.         $high=addslashes(str_replace('$', '', $high));
  8.             $low=addslashes(trim($low));
  9.         $high=addslashes(trim($high));
  10.  
  11.         if(!is_numeric($low)){
  12.             echo "low price is not a number<br/>";
  13.             settype($low, "integer");
  14.         }
  15.         if(!is_numeric($high)){
  16.             echo "high price is not a number<br/>";
  17.         }
  18.             echo "$low, $high <br/>";
  19.  
The really strange part is that the second value is returned as a number, its only the first one that isn't.
Feb 1 '09 #1
7 4680
dlite922
1,584 Expert 1GB
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
Feb 1 '09 #2
pedalpete
110 100+
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?
Feb 1 '09 #3
dlite922
1,584 Expert 1GB
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?



@pedalpete
Feb 1 '09 #4
TheServant
1,168 Expert 1GB
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]?
Feb 1 '09 #5
pedalpete
110 100+
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?
Feb 1 '09 #6
Atli
5,058 Expert 4TB
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.
Feb 1 '09 #7
pedalpete
110 100+
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
Feb 2 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Steven Bethard | last post by:
Is there a good way to determine if an object is a numeric type? Generally, I avoid type-checks in favor of try/except blocks, but I'm not sure what to do in this case: def f(i): ... if x < i:...
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
7
by: Raphi | last post by:
Hi, I'm trying to clean up a large database in Access. I have one field for address, which needs to be broken up into Street Number, Street Name, and Street Label (St., Road, etc.) The...
0
by: Josh Harris | last post by:
Here is my issue: I have a datagrid that is populated with a datatable. I want the columns of the datagrid to be sortable. I also want to format the numeric columns such as two decimal places...
10
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
5
by: JSParker1 | last post by:
Summary: Maximum number of records per second that can be inserted into SQLServer 2000. I am trying to insert hundreds (preferably even thousands) of records per second in to SQLServer table...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
8
by: Elfae | last post by:
I have searched high and low for a sample for this, and I just can't find any. Sorry for the length! Background Information The issue revolves around setting up a system-generated increase in...
10
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: pgharat | last post by:
Hello, I am working on Excel 2007 VBA. I have around 7-8 excel 2007 template in which using VBA added all functionality. In that some of the excel templates i m facing some issue of number...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.