Converting string to number problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • acsandras@gmail.com

    Converting string to number problem

    I am trying to extract data from an iso-8859-1 encoded XML document. I
    open the file, and read each line after each other, and extract some
    values. These values are behaving oddly, though.

    I can display them as strings:
    echo $value; // Returns 0.4
    echo "'$value'"; // Returns '0.4'

    But I can not convert them into float:
    echo floatval($value ); // Returns 0

    I did some debugging, which shows that the string length is 18, and not
    3 as I axpected.
    echo strlen($value); // Returns 18

    The encoding is ASCII:
    echo mb_detect_encod ing($value); // Returns ASCII

    Does anybody have an idea what this could be? I am using PHP 4.3.4.
    Thanks in advance, best regards;
    AndrĂ¡s

  • Ewoud Dronkert

    #2
    Re: Converting string to number problem

    acsandras@gmail .com wrote:[color=blue]
    > I did some debugging, which shows that the string length is 18, and not
    > 3 as I axpected.[/color]

    Try

    for ( $i = 0; $i < strlen($v); ++$i )
    printf(' %02d', ord($v{$i}));

    and see what comes out.

    --
    E. Dronkert

    Comment

    • Super Mario

      #3
      Re: Converting string to number problem

      The problem was that I tried to send the following string into the
      function:

      "<value>0.4 </value>"

      When echoing the value and viewing the results in IE, IE parsed <value>
      as an HTML tag, and only showed '0.4'. I changed headeren to return
      text/plain document, and I realised the problem this way.

      Comment

      • Ewoud Dronkert

        #4
        Re: Converting string to number problem

        Super Mario wrote:[color=blue]
        > "<value>0.4 </value>"[/color]

        Oh right :) Well, always do at least this when investigating string
        contents: echo '<pre>'.htmlspe cialchars($s).' </pre>' (or indeed, change
        content-type to text/plain).

        --
        E. Dronkert

        Comment

        Working...