How to convert string to integer with (int)/intval?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luftikus143
    New Member
    • Jan 2007
    • 97

    How to convert string to integer with (int)/intval?

    Hi there,

    gush, frustrating. I am not a pro; but I know PHP well enough to understand how it works. And a simple conversion from string to integer via (int)$value or intval($value) should be straightforward . But it's not working in my case.

    I have a string:
    Code:
    % population: 99%
    which holds some information I extract via preg_match.

    The resulting variable $r[0] is "99". I have an
    Code:
    if (($r[0] >= 75))
    which used to work. But I don't know for what reason, that doesn't do it anymore.

    I tried all combinations of (int) and intval()
    Code:
    $t = (int)$r[0];
    $t = intval($r[0];
    , but in vain. It treats my variable afterwards as 0.

    What is wrong here?!

    Thanks for any hints!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    depending on your RegEx, $r[0] might not be "99" any more.

    Comment

    • luftikus143
      New Member
      • Jan 2007
      • 97

      #3
      Thanks. But I checked that, and the value of $r[0] is "99". But it won't convert into a number.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what does var_dump($r[0]); give you?

        Comment

        • luftikus143
          New Member
          • Jan 2007
          • 97

          #5
          I am not in the office right now. Will check tomorrow. Thanks already for the hints!

          Comment

          • luftikus143
            New Member
            • Jan 2007
            • 97

            #6
            Ok, so, it gives me a
            Code:
            string(5) "99"
            . What does that mean? That the value is 5 characters long? Why would that be?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              exactly, you could have some whitespace before and/or after. you could try trimming the value beforehand.

              PS. verify the output in the (HTML) source, as HTML itself trims whitespace in its rendering.
              Last edited by Dormilich; Dec 2 '10, 08:52 AM.

              Comment

              • luftikus143
                New Member
                • Jan 2007
                • 97

                #8
                I did try as well the "trim" the other day, but nothing did change...

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  well, you need to find out, why the string length is given as 5.

                  Comment

                  • luftikus143
                    New Member
                    • Jan 2007
                    • 97

                    #10
                    Strange, strange. I used a trim, but nothing changed. It seems that when the number has two places (i.e >= 10), it turns from string(4) to string(5).

                    string(4) "9"
                    string(5) "22"

                    Perhaps a clue for someone? I am really stuck. No idea what to do.

                    Thanks a lot for any hints!

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      what does this look like in the (HTML) source code?

                      EDIT: do you use UTF-8? try running htmlentities() (with the appropriate encoding) on it.
                      Last edited by Dormilich; Dec 6 '10, 03:06 PM.

                      Comment

                      • luftikus143
                        New Member
                        • Jan 2007
                        • 97

                        #12
                        Ha, jackpot! And I feel stupid that I haven't looked in the HTML code myself before. Indeed, there is a "<b>9" and "<b>22", instead of the pure number. Thanks a thousand times for this hint! Great!!

                        Comment

                        Working...