Thread subject: Official Home of the PHPFusion CMS :: Parsing A Text File

Posted by afoster on 13-07-2022 23:04
#1

I have a text file containing data in one continuous line separated by a comma (,). I'm looking for php code that will parse that data into either one or preferably two columns. All I have been able to do at this point is pull the data into a <td> that displays the data as a continuous line. Any input would be appreciated.

Posted by Chan on 14-07-2022 09:15
#2

I'm not sure what are you looking for in responses, or where you encountered difficulty in solving, so here's my few grain of salt in this.

If you are splitting the text separated by commas only:
Code
$sentences = explode(',', $text);


What problem I see here, is that your text cannot contain a single comma since the comma symbol is used as a delimiter, which 9/10 of the chance won't work nicely. I use multiple commas in a single sentence at times.

Then, if you want to join up the $sentences array into a subgroup, you can group them up further using array_chunk function

Then I think it's best not to work with <td>, those are for excel kind of use for storing hard statistical data use only where you need to search value by columns. Secondary, they are not responsive to screen sizes.

By way of splitting them into 2 columns, you have few choices to do this.

1. Using Bootstrap
Code
<div class='row'><div class='col-xs-12 col-sm-6'>".implode("</div><div class='col-xs-12 col-sm-6'>", $sentences)."</div></div>


2. Using Grid CSS
Code

.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap:15px;
}
<div class="grid"><div>".implode("</div><div>", $sentences)."</div></div>


If you do not want to limit to 2 columns, you can go for a more linear flex type of div container. It is when we need content to stack downwards to next row automatically once the content goes off screen.

Edited by Chan on 11-07-2026 16:14

Posted by afoster on 14-07-2022 14:00
#3

Thank you so much for your response Chan...unfortunately much of what you wrote is way over my head. I do have a comma after each piece of data so the implode should work for me. I will do some further googling to see if I can find some sample code that may lead me to my solution. I should mention that the data in the text file are email addresses separated by a comma.

Edited by afoster on 14-07-2022 16:27

Posted by Grimloch on 17-07-2022 20:52
#4

Fred; if you have figured this out let us know if not; email me and I can help you with it.

Posted by afoster on 18-07-2022 03:11
#5

Yes, I was able to get some help elsewhere so I am good to go. Just got it set it up today, so you can delete this thread.

Grimloch, tried to send you a private message but it would not go through because your inbox is full.

Posted by Grimloch on 18-07-2022 03:48
#6

OK thanks; I'll take care of it.