Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a looped string using PHP (break a string into an array)

Status
Not open for further replies.

dreamaz

Technical User
Dec 18, 2002
184
CA
Hello,

I would like to split the string and have the results in an array which I can then loop until all are displayed. This is what I currently have thanks to Jeff.

<?php
$BREAK = str_replace(",", " ", $_POST['feild1']);
for ($loop = 0; $loop < count($BREAK); $loop++) echo '<font size="2">'.nl2br($BREAK);
?>

How can I have all the values in $BREAK split into arrays so I can have the data spread across a table columns? I used $BREAK = explode(",", $_POST['feild1']); before and got the arrays, but only the first line was displayed thefore had to use the loop, but then I can't use the arrays? Any help is appreciated.

Thanks,

dR

 
If you are trying to break a string into an array of elements, I recomend taking a look at PHP's explode() function.

This will give you an array which can be used with a foreach loop:

Code:
<?php
print '<html><body><table><tr>';
$pieces = explode(',', $_POST['field1']);

foreach ($pieces as $piece)
	print '<td>' . $piece . '</td>';
	
print '</tr></table></body></html>';
?>



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for the reply,

How do i call/print each array separately so i can organize the data in different views? With the previous explode command without the loop/foreach i had something like the following:

<?php
echo $piece[0]
echo $piece[2] ?>

(so i don't need the second object $piece[1] I would like to manipulate the displayed data)

Thanks,

dR
 
The pieces will be in a numerical array. You can manipulate $pieces[0] or $pieces[1] or whathaveyou.

Doing:

Code:
<?
$pieces = explode(',', $_POST['field1']);
print_r ($pieces);
?>

will tell you a lot about the contents of $pieces.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
OK so if i have a maximum of 28 values in one line, and want a new array to be created for 29-56 and 57-84 (these will be dynamically created until there is no more data to process) but ideally I would like array[0]-array[27] and then the next value should automatically go into a new array and so forth.

I am trying to have a script where people can cut/paste a log file split with CSV and then have the output displayed in a table format. So when multiple lines of the log file are pasted, every 28th value should be a new row

Is this possible? Your help is appreciated


thanks,

dR
 
Now Im REALLY lost! This is what i had before which works great, but it's all in one string:

<?php if(isset($_POST['feild1'])){
$BREAK = str_replace(",", " . ", $_POST['feild1']);
for ($loop = 0; $loop < count($BREAK); $loop++) echo '<font size="2">'.nl2br($BREAK); }
?>

So i get the output i want except due to the length of data varying in some fields, it throws the alignment off from the Table titles. ie 2 fields are measured in bytes which varies, here is where the display starts to look staggered. So i thought if i can still have a the same output, but control the table columns that are created, that way at least the view is maintained and the table titles for each column match the data below it.

The code you provided in your first response also gave me a view similar to the code above, but somewhat had the same problems with the alignment once i got to the BYTES-IN AND BYTES OUT view.

thanks

dR

 
No offense, but you would likely have better success asking for what you actually want. Do you remember posting, "I would like to split the string and have the results in an array which I can then loop until all are displayed"?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
What is it, exactly, you want to do?

I infer that you are taking in a string, splitting it at the commas, and outputting those parts somehow. The output will, apparently, not be all on one page.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ok sorry for the confusion.

I have this as my raw data which I then want to have displayed in a table while maintaining a proper view.

12/06/2005,15:41:08,172.10.55.10,Stop,1234567890@mike.com,200,1234567890@mike.com,172.10.55.10,36868,10.10.10.26,000001234567890,172.10.55.10,2,21965,57770,183,1,,,,,,,1133883668,,312400000B61,1c-3kjeX,1c-3kjeW,59

The above is basically an accounting log that tracks a user's session on our system, problem is that its hard for some people here to read and figure out what's what.

I was hoping to create a script that would strip the commas, and output the values in a tabled format. Now there are situations where there will be multiple lines of data (usually 5-6 lines) which I need to each to appear in its own row. There are 28 fields in each line of data split by commas. I currently have a textfield called feild1 where someone can cut and paste their data and then click on SUBMIT which should then reassemble the data in a view that can be read.

Now i did manage to get something going, but my problem there was that the last few fields in each line, the data size can vary which was throwing off alignment towards the end and the result was not falling under the title. If I can get that to remain a fixed size then that would be OK for me as well.

Hope this helps, i appreciate your help and apologize for the confusion.

Thanks,

dR
 
I recommend that you use the "limit" parameter with explode(). (
With that, you could specify that the line be split at the commas, but that only a certain number of commas are to be processed.

For example, on my system the script:

Code:
<?php
$a = '12/06/2005,15:41:08,172.10.55.10,Stop,1234567890@mike.com,200,1234567890@mike.com,172.10.55.10,36868,10.10.10.26,000001234567890,172.10.55.10,2,21965,57770,183,1,,,,,,,1133883668,,312400000B61,1c-3kjeX,1c-3kjeW,59';

$b = explode (',', $a);
print_r ($b);

$b = explode (',', $a, 10);
print_r ($b);
?>

outputs:
[tt]Array
(
[0] => 12/06/2005
[1] => 15:41:08
[2] => 172.10.55.10
[3] => Stop
[4] => 1234567890@mike.com
[5] => 200
[6] => 1234567890@mike.com
[7] => 172.10.55.10
[8] => 36868
[9] => 10.10.10.26
[10] => 000001234567890
[11] => 172.10.55.10
[12] => 2
[13] => 21965
[14] => 57770
[15] => 183
[16] => 1
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] =>
[23] => 1133883668
[24] =>
[25] => 312400000B61
[26] => 1c-3kjeX
[27] => 1c-3kjeW
[28] => 59
)
Array
(
[0] => 12/06/2005
[1] => 15:41:08
[2] => 172.10.55.10
[3] => Stop
[4] => 1234567890@mike.com
[5] => 200
[6] => 1234567890@mike.com
[7] => 172.10.55.10
[8] => 36868
[9] => 10.10.10.26,000001234567890,172.10.55.10,2,21965,57770,183,1,,,,,,,11
33883668,,312400000B61,1c-3kjeX,1c-3kjeW,59
)
[/tt]

If some of the last values may or may not be there, pick a point where you know the values are always there and only explode the string to that point.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Here's a more complete offering, which can take multiple lines of data in a variable, split it into individual lines, split the lines into pieces and output the pieces in a table:

Code:
<?php
$a ='a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p
b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q
c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r
d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s
e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t';

$a_array = preg_split ('/(\r\n)|(\n)|(\r)/', $a);

print '<html><body><table border="1">';

foreach ($a_array as $a_line)
{
	print '
	<tr>';
	$a_data = explode (',', $a_line, 10);
	
	foreach ($a_data as $a_datum)
	{
		print '<td>';
		if (strlen($a_datum) == 0)
			print '&nbsp;';
		else
			print $a_datum;
	}
}

print '</table></body></html>';
?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for this and all your help.. Im going to spend some time tinkering around with things

dR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top