Page 1 of 1

loop script help

Posted: Wed Nov 27, 2002 7:41 pm
by hawkeye177
Hi.

I'm trying to make a script that reads the information and splits it into different pieces.. That part i have finished... But i need help making the script advance to next line and read more data..... Here is what my script looks like

Code: Select all

<?php
$data = '1d3b f0513ae0 a9378695 00000004 0292 79f31bf1 f57a235b 9c182544 0e9f 3ed08b21 e7dda080 8e5a1355 008c 60d24a5f b404c4bf 23800ddb 1041133120 24
0c61 4cdd686a 4c7eb9cc 00000004 005c 52e0bb3a a966f4dd d21d27e7 04e8 436852a6 be71fb29 e3f4af74 03c8 2ff7736f 695448ab fadf67e7 84792832 9
0847 f922650b 741ced93 00000000 0b62 ea0f59fa 8457ee92 73524c0e 01e9 bb30e709 80dc2e50 7ed6eac3 0f6b 97a88f2e b38bc69a 58f6afe3 815187072 15
084f f307786b c2d8c5ea 00000008 094c ff1bd26a 6f8a3c06 b5ab7410 0776 5b4696e5 cbbc4c4e 626e2fd5 0b29 937b75c5 f792f97b 300a9112 308651648 3
1953 5112d66c c85cbb30 0000000c 0ff4 dbc80ff6 3303938a 5d1d6a72 0513 ff1b1c7b 77088b76 dfc21714 0220 8a044bc7 51bf862b c8d3b394 1213762496 12';
$x = substr("$data", 0, 31);    
 
$pct = substr("$data", 64, 31); 

$qct = substr("$data", 96, 31); 

$final = print("x= 0000$x <br> pct= 0000$pct <br>qct= 0000$qct");

?>
Can anybody help me make it loop and print the next lines information?
thanks
hawkeye177

Posted: Thu Nov 28, 2002 12:42 am
by volka
you might use explode or preg_split or since you know the length of each line something like

Code: Select all

$data = substr($data, $linelength);
while strlen($data) > $linelength.
Also note that substr("$data", ...) is unnecessary, substr($data, ...) will do.
Read http://www.php.net/manual/en/language.t ... tax.double and http://www.php.net/manual/en/language.t ... ng.parsing to learn more about this

Posted: Thu Nov 28, 2002 11:34 am
by hawkeye177
I still can't get it to work :(
When i try some of your things volka, i get some errors... I'm a n3wb.
Let me explain what i want done and maybe you can help me more.
I want the first line to print this
x= 00001d3b f0513ae0 a9378695 00000004
pct= 00000e9f 3ed08b21 e7dda080 8e5a1355
qct= 0000008c 60d24a5f b404c4bf 23800ddb
The second to print this
x= 00000c61 4cdd686a 4c7eb9cc 00000004
pct= 000004e8 436852a6 be71fb29 e3f4af74
qct= 000003c8 2ff7736f 695448ab fadf67e7
and so on until it has no more data
can you help me do something like this?

Posted: Thu Nov 28, 2002 1:21 pm
by volka
e.g.

Code: Select all

&lt;?php
$data1 = '1d3b f0513ae0 a9378695 00000004 0292 79f31bf1 f57a235b 9c182544 0e9f 3ed08b21 e7dda080 8e5a1355 008c 60d24a5f b404c4bf 23800ddb 1041133120 24
0c61 4cdd686a 4c7eb9cc 00000004 005c 52e0bb3a a966f4dd d21d27e7 04e8 436852a6 be71fb29 e3f4af74 03c8 2ff7736f 695448ab fadf67e7 84792832 9
0847 f922650b 741ced93 00000000 0b62 ea0f59fa 8457ee92 73524c0e 01e9 bb30e709 80dc2e50 7ed6eac3 0f6b 97a88f2e b38bc69a 58f6afe3 815187072 15
084f f307786b c2d8c5ea 00000008 094c ff1bd26a 6f8a3c06 b5ab7410 0776 5b4696e5 cbbc4c4e 626e2fd5 0b29 937b75c5 f792f97b 300a9112 308651648 3
1953 5112d66c c85cbb30 0000000c 0ff4 dbc80ff6 3303938a 5d1d6a72 0513 ff1b1c7b 77088b76 dfc21714 0220 8a044bc7 51bf862b c8d3b394 1213762496 12';

$data = preg_split("!\r|\n!", $data1, -1, PREG_SPLIT_NO_EMPTY);
foreach($data as $line)
{

	$x = substr($line, 0, 31);   
	$pct = substr($line, 64, 31);
	$qct = substr($line, 96, 31);
	echo $line, '&lt;br/&gt;x= 0000',$x ,'&lt;br/&gt;pct= 0000', $pct, '&lt;br/&gt;qct= 0000', $qct, '&lt;hr/&gt;';

}
echo 'done.';
?&gt;
http://www.php.net/manual/en/function.preg-split.php
http://www.php.net/manual/en/control-st ... oreach.php

Posted: Thu Nov 28, 2002 7:31 pm
by hawkeye177
Sweetness!!!! it works! thank you soo much!


I got one more question... how can i make this "echo $line, '<br/>x= 0000',$x ,'<br/>pct= 0000', $pct, '<br/>qct= 0000', $qct, '<hr/>'; " display more places in the script?
thanks:)

Posted: Thu Nov 28, 2002 9:45 pm
by volka
uh?

Posted: Sat Nov 30, 2002 1:13 pm
by hawkeye177
Nevermind:) I got it:)