SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert String to Float

    I've always thought explicit type casting was as easy as (type)me ... but that's not the case here.

    Basically, a number is trapped between the bold tag and Items. I preg-match it, and $ytotalnumber outputs the right # ... but, run the code, and though it displays the right type, it displays 0 (with or without the operation)!

    Is there a way I can convert the numbers in the "1114" string to float?

    PHP Code:
    $txt=<<<ENDNOW
    <b>
    1114 Items
    ENDNOW;


     
    preg_match("/<b>\r\n[0-9]* Items/",$txt,$match) ;
     
    $ytotalnumber trim(str_replace("Items","",$match[0]));

     
    $ypages2load $ytotalnumber;
     
    $ypages2load = (float)$ypages2load;
     
     echo 
    gettype($ypages2load);
     echo 
    $ypages2load/2
    tnx

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Location
    London, UK
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have played around with your script, and am baffled as to why 1114/2 produces 0, regardless of whether you try to cast it. However, it's not normally necessary to cast a string to a number when performing a calculation. PHP does it implicitly. I have simplified your script, and the following produces the desired result:
    PHP Code:
    <?php
     $txt
    =<<<ENDNOW
    <b>
    1114 Items
    ENDNOW;

    // Use parentheses in the regex to capture the numbers
    preg_match('/<b>\s+(\d+)\s+Items/',$txt,$match);
    $ypages2load $match[1];
    // Although $ypages2load is a string, PHP converts it implicitly
    // to a number when dividing by 2.
    echo $ypages2load/2;
    ?>

  3. #3
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, so it was my regular expression and cheap str_replace trick at fault? actually, i think it might have been str_replace ... str_replace probably did something strange to the string such that implicit type casting (and even explicit!) wouldn't work anymore.

    PHP Code:
    $number="<b>11435</b>";
    $rnumber=str_replace("11435","35",$number);

    //echo $rnumber;
    echo $rnumber/2

  4. #4
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you believe you get 0 as the output even if you

    echo (float)$rnumber/2;

    ?!

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jackli
    can you believe you get 0 as the output even if you

    echo (float)$rnumber/2;

    ?!
    Yes, that's normal, read about string conversion to numbers. You gotta get rid of the tags first:

    PHP Code:
     $number="<b>11435</b>";
    $rnumber=str_replace("11435","35",$number);

    //echo $rnumber;
    echo strip_tags($rnumber)/2
    Edit:
    Actually looking at your original post:
    PHP Code:
     $txt=<<<ENDNOW
    <b>
    1114 Items
    ENDNOW;


    preg_match("/<b>\r\n([0-9]*) Items/",$txt,$match) ;
    $ytotalnumber $match[1];

    echo 
    $ytotalnumber /2
    Saul

  6. #6
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ack. i was too quick in assuming it was the str_replace at fault. my haphazard experiment was very badly conducted!

    but so basically it's the round-parentheses in ([0-9]) in the reg expression that forces the expression to convert properly to from string to number?!

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jackli
    ack. i was too quick in assuming it was the str_replace at fault. my haphazard experiment was very badly conducted!

    but so basically it's the round-parentheses in ([0-9]) in the reg expression that forces the expression to convert properly to from string to number?!
    The conversion occurs later, upon arithmetical operation. The parentheses select just the number to make the conversion possible.
    Saul

  8. #8
    SitePoint Enthusiast Salkcin's Avatar
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    PHP Code:
    <?php

    $txt
    =<<<ENDNOW
    <b>
    1114 Items
    ENDNOW;


    preg_match('/\d+/s'$txt$match);
    $ypages2load $match[0];

    echo 
    $ypages2load/2// Output: 557

    ?>

  9. #9
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just trying to understand exactly how this works -- but, doesn't the not-so-elegant hack of trim() and str_replace() do the equivalent of selecting just the number via regex?

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jackli
    just trying to understand exactly how this works -- but, doesn't the not-so-elegant hack of trim() and str_replace() do the equivalent of selecting just the number via regex?
    Probably yes, but why do you need that? Select just what you need.
    Saul

  11. #11
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's an excuse for not knowing more than the barebones-barebones of reg ex
    (but of course i should spend more time on learning regex!)

    the trim and str_replace hack was actually what i tried in my first post; the conversion wouldn't work somehow!

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jackli
    it's an excuse for not knowing more than the barebones-barebones of reg ex
    (but of course i should spend more time on learning regex!)

    the trim and str_replace hack was actually what i tried in my first post; the conversion wouldn't work somehow!
    Ah, I see it. It would work if you stripped the tag (<b>) too.
    Saul


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •