number not numeric issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pedalpete
    New Member
    • Oct 2007
    • 109

    number not numeric issue

    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?

    Code:
    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.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    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

    Comment

    • pedalpete
      New Member
      • Oct 2007
      • 109

      #3
      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?

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        only an invalid value causes a 0 in MySQL.

        Did you try the die(var_dump($h igh) . " " . var_dump($low)) ; debug print line I told you?



        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?

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          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]?

          Comment

          • pedalpete
            New Member
            • Oct 2007
            • 109

            #6
            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?

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              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.

              Comment

              • pedalpete
                New Member
                • Oct 2007
                • 109

                #8
                I should have known that Atli

                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

                Comment

                Working...