Reputation:
I have made a script which reads all CSV files from a folder, puts the data to my database and moves all the csv file to another folder afterward.
I get emails with tables which are meant to put it into a CSV file, I am trying to create a script which reads my mail, only shows the emails with tables and converts the text into a CSV file, moves the file to my folder, reads it and transfers the data. The biggest part is done already, but I am struggling with this bit.
After filtering the mail and having the table, I took away the HTML tags (<table>, <p>, <tr> etc.)
I am replacing the </td>
with tabs because it has to separate the rows, but on the last </td>
it also adds a tab and that makes my script unable to read the CSV files because the separator are tabs and on the last tab there is no value given.
This is my HTML table after removing all the tags with string and preg replace, and as you see, it has a tab on the end (Sorry for hiding some text)
I have tried some rtrims and substr, but I haven't been able to be doing this for each line.
echo substr($key, 0, -1);
Only takes away the tab on the last line, it is 1 full string so I am not sure if it is possible.
If any more code is needed then I'd like to hear it. Sorry for the long question, any help would be appreciated!
Thanks =)
Upvotes: 0
Views: 1388
Reputation:
I have solved my own question.
I am replacing all my <td>
with tabs, and every 8th tab gets replaced with nothing, so I'm removing it.
This is the code that I've used:
$res_str = array_chunk(explode("\t",$outputstr),8);
foreach( $res_str as &$val){
$val = implode("\t",$val);
}
echo implode("",$res_str);
I'm so happy actually, took me so long to first even analyze the problem for not being able to read the CSV file and then fixing it.
Upvotes: 0
Reputation: 7485
You could try the m (PCRE_MULTILINE) modifier in a regex.
This should strip trailing space from every line of your string (below $subject). If you didn't use the m modifier, it would only remove the trailing space at the end of the string.
<?php
$pattern = "@\s$@m";
$subject = "'a'\t'b'\t'c'\t\n'd'\t'e'\t'f'\t\n";
$out = preg_replace($pattern, '', $subject);
print $out;
Output:
'a' 'b' 'c'
'd' 'e' 'f'
Do try it on your subject as the output is hard to read here.
Upvotes: 0
Reputation: 75
Try str_replace functions with \t\n characters:
str_replace("\t\n","\n", $string);
Upvotes: 1