Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: Format String

  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Format String

    I need a script that will perform a few simple tasks:

    First, it will need to access an external page, which contain a list of numbers separated by commas:
    Code:
    218223,1360,18451694 798091,65,482038 722209,61,317603 -1,-1,-1 932495,64,447023 -1,-1,-1 729094,45,65150 194463,76,1352096 802228,60,298013 13532,95,9049500 115728,85,3269373 947947,59,254111 709509,50,109165 154893,63,373373 389971,55,175535 284102,68,614631 62843,61,303020 335884,50,110623 686621,42,45904 261504,48,87979 81721,53,143396 66559,61,311642 253043,40,39255 139017,50,101380
    Then I will need it to isolate a specific number from that, ex. 9049500, and divide it over another number to get a percentage, ex. with 9049500 it would divide it by another number such as 20000000 to get 45.2475 and establish it [the percentage] as a variable.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Do the numbers always fall in the same place (within the commas),
    and do you know which position the numbers are that you need to use?

    And are there a bunch of lines like the one you showed?

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Do the numbers always fall in the same place (within the commas),
    and do you know which position the numbers are that you need to use?

    And are there a bunch of lines like the one you showed?
    Yes, the numbers will always be in the same place within the commas. Right now, I would specifically need to use the 29th and 30th numbers (95 and 9049500, respectively). I assume this would be fairly easy to edit in the future if the need arose.

    The example in my first post is the actual string itself, and it's the only one I'd use. Those numbers update on the external site on a frequent basis, hence the need to retrieve it from there.

    The numbers are actually in sets of three, ex. [218223,1360,18451694], [-1,-1,-1], and [13532,95,9049500] are all sets. Maybe an array could be used for the A, B, and C of each set? Otherwise retrieving individual numbers would be fine.
    Last edited by coolcamo8642; 11-19-2007 at 12:41 AM.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    $str '218223,1360,18451694 798091,65,482038 722209,61,317603 -1,-1,-1 932495,64,447023 -1,-1,-1 729094,45,65150 194463,76,1352096 802228,60,298013 13532,95,9049500 115728,85,3269373 947947,59,254111 709509,50,109165 154893,63,373373 389971,55,175535 284102,68,614631 62843,61,303020 335884,50,110623 686621,42,45904 261504,48,87979 81721,53,143396 66559,61,311642 253043,40,39255 139017,50,101380';
    $arr explode(','str_replace(' '','$str));
    echo 
    "{$arr[29]} / {$arr[28]} = " $arr[29] / $arr[28]; 
    I added a str_replace() to replace the spaces with commas so each number would be in it's own index.

  • Users who have thanked Inigoesdr for this post:

    coolcamo8642 (11-19-2007)

  • #5
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    I added a str_replace() to replace the spaces with commas so each number would be in it's own index.
    Thanks; that works wonderfully! The only problem is that it doesn't load the numbers from the external page. If I wanted to load the values from "example.com" and put that inside of the str, what code would I use? The external page only has the raw string in my first post, HTML-free, which I would think would simplify things a bit.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you host has allow_url_fopen enabled you can do this:
    PHP Code:
    $str file_get_contents('http://example.com/page.txt'); 

  • Users who have thanked Inigoesdr for this post:

    coolcamo8642 (11-20-2007)

  • #7
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    If you host has allow_url_fopen enabled you can do this...
    I've gotten allow_url_fopen enabled and it's successfully grabbing the string remotely. However, there is a new problem.

    I'm currently looking at:
    PHP Code:
    $arr explode(','str_replace(' '','$str));

    echo 
    $arr [29];
    echo 
    $arr [30]; 
    It returns [55175535 284102], which are the 44th, 45th, and 46th numbers (44th and 45th joined). For clarification, in the original string it was [389971,55,175535 284102,68,614631], whereas the numbers I wanted it to get were 95 and 9049500 (The 29th and 30th - [13532,95,9049500]).

    I've tried a couple of different things to replace the commas but it only worsens the issue.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Post the whole string, and print_r() the array after it's exploded.
    PHP Code:
    echo '<pre>' print_r($arr,1); 

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I know this is a minor script, but I didn't know we were doing free work now. I should post some work that I have left! Usually questions such as these are followed by, "What have you done so far?"

  • #10
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    Post the whole string, and print_r() the array after it's exploded.
    Great, it makes a lot more sense now...some of the numbers have changed but the structure seems pretty apparent:
    Code:
    Array
    (
        [0] => 218793
        [1] => 1360
        [2] => 18637775
    799702
        [3] => 65
        [4] => 482038
    723841
        [5] => 61
        [6] => 317603
    -1
        [7] => -1
        [8] => -1
    934812
        [9] => 64
        [10] => 447023
    -1
        [11] => -1
        [12] => -1
    730738
        [13] => 45
        [14] => 65150
    194926
        [15] => 76
        [16] => 1352207
    803999
        [17] => 60
        [18] => 298013
    13218
        [19] => 95
        [20] => 9234500
    ...
    )
    However, when I use:
    PHP Code:
    echo $arr[19];
    echo 
    $arr[20]; 
    It returns: [959234500 116004] whereas it should return [95 9234500].

    The reason for this might be that in the actual source of the file with the string, the sets are separated onto different lines:
    Code:
    218810,1360,18637775
    799775,65,482038
    723916,61,317603
    -1,-1,-1
    934924,64,447023
    -1,-1,-1
    730809,45,65150
    194942,76,1352207
    804084,60,298013
    13220,95,9234500
    But since it contains no HTML the linebreaks just appear as spaces when viewing in a browser. I'm tried using str_replace to remove linebreaks but it doesn't appear to have changed anything.
    Last edited by coolcamo8642; 11-21-2007 at 12:32 AM.

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Try
    PHP Code:
    str_replace("\n"','$string); 

  • Users who have thanked Inigoesdr for this post:

    coolcamo8642 (11-21-2007)

  • #12
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    Try...
    Thanks, I'll post progress in a minute...
    Last edited by coolcamo8642; 11-21-2007 at 02:37 AM.

  • #13
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You can't use str_replace() like that; that was just an example. Try something like this:
    PHP Code:
    $find = array(' '"\n"); 
    $arr explode(','str_replace($find','$str)); 

  • Users who have thanked Inigoesdr for this post:

    coolcamo8642 (11-21-2007)

  • #14
    New Coder
    Join Date
    Jun 2006
    Posts
    67
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    You can't use str_replace() like that; that was just an example. Try something like this:
    PHP Code:
    $find = array(' '"\n"); 
    $arr explode(','str_replace($find','$str)); 
    Excellent! Now it works perfectly!
    Code:
    Array
    (
        [0] => 218849
        [1] => 1360
        [2] => 18666495
        [3] => 799866
        [4] => 65
        [5] => 482038
        [6] => 723990
        [7] => 61
        [8] => 317603
        [9] => -1
        [10] => -1
        [11] => -1
        [12] => 935049
        [13] => 64
        [14] => 447023
        [15] => -1
        [16] => -1
        [17] => -1
        [18] => 730897
        [19] => 45
        [20] => 65150
        [21] => 194960
        [22] => 76
        [23] => 1352207
        [24] => 804214
        [25] => 60
        [26] => 298013
        [27] => 13171
        [28] => 95
        [29] => 9262500
        [30] => 116020
        [31] => 85
        [32] => 3269922
        [33] => 950363
        [34] => 59
        [35] => 254111
        [36] => 711746
        [37] => 50
        [38] => 109165
        [39] => 155102
        [40] => 63
        [41] => 373793
        [42] => 390692
        [43] => 55
        [44] => 175535
        [45] => 284661
        [46] => 68
        [47] => 614632
        [48] => 63081
        [49] => 61
        [50] => 303020
        [51] => 336475
        [52] => 50
        [53] => 110623
        [54] => 687900
        [55] => 42
        [56] => 45904
        [57] => 261134
        [58] => 48
        [59] => 88699
        [60] => 82051
        [61] => 53
        [62] => 143396
        [63] => 66744
        [64] => 61
        [65] => 311642
        [66] => 254204
        [67] => 40
        [68] => 39255
        [69] => 139420
        [70] => 50
        [71] => 101380
        [72] => -1
        [73] => -1
        [74] => 
    )
    Thanks so much for all of your help!


  •  

    Posting Permissions

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