...

View Full Version : Format String



coolcamo8642
11-18-2007, 07:58 PM
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:

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.

mlseim
11-19-2007, 12:29 AM
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?

coolcamo8642
11-19-2007, 01:20 AM
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.

Inigoesdr
11-19-2007, 01:46 AM
$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.

coolcamo8642
11-19-2007, 02:13 AM
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. :)

Inigoesdr
11-19-2007, 03:22 AM
If you host has allow_url_fopen enabled you can do this:

$str = file_get_contents('http://example.com/page.txt');

coolcamo8642
11-20-2007, 05:09 AM
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:


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

Inigoesdr
11-20-2007, 07:56 AM
Post the whole string, and print_r() the array after it's exploded.

echo '<pre>' . print_r($arr,1);

aedrin
11-20-2007, 04:36 PM
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?"

coolcamo8642
11-21-2007, 12:07 AM
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:


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:


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:


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.

Inigoesdr
11-21-2007, 02:36 AM
Try
str_replace("\n", ',', $string);

coolcamo8642
11-21-2007, 03:06 AM
Try...
Thanks, I'll post progress in a minute...

Inigoesdr
11-21-2007, 03:38 AM
You can't use str_replace() like that; that was just an example. Try something like this:

$find = array(' ', "\n");
$arr = explode(',', str_replace($find, ',', $str));

coolcamo8642
11-21-2007, 03:53 AM
You can't use str_replace() like that; that was just an example. Try something like this:

$find = array(' ', "\n");
$arr = explode(',', str_replace($find, ',', $str));
Excellent! Now it works perfectly!


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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum