Support the ongoing development of Laravel.io →
posted 10 years ago
Packages
Last updated 1 year ago.
0

You probably don't need a package for this; just basic PHP string/array manipulation.

$csv = 
   "apple,pear,pineapple,durian
   orange,banana,papaya,coconut";

// replace each comma with four spaces
$columns = str_replace(",", "    ", $csv);
Last updated 1 year ago.
0

Also, if you're using this output in a view it may be helpful to split it into an array of rows:

$csv = 
   "apple,pear,pineapple,durian
    orange,banana,papaya,coconut";

// split into rows
$rows = explode("\n", $csv);

$columns = array();

// replace each comma with four spaces
foreach($rows as $row) {
	$columns[] = str_replace(",", "    ", $row);
}

print_r($columns);
Last updated 1 year ago.
0

benjamincohenbaritone said:

Also, if you're using this output in a view it may be helpful to split it into an array of rows:

$csv = 
  "apple,pear,pineapple,durian
   orange,banana,papaya,coconut";

// split into rows
$rows = explode("\n", $csv);

$columns = array();

// replace each comma with four spaces
foreach($rows as $row) {
  $columns[] = str_replace(",", "    ", $row);
}

print_r($columns);

I believe it is better or more reliable to use array_chunk in this case instead of basing rows on an arbitrary character "\n", that could yield inconsistent results :)

Last updated 1 year ago.
0

That's a good point, assuming you know beforehand the number of columns. But I think a well-formed csv file is supposed to have newline characters? Not really sure.

Last updated 1 year ago.
0
Solution

The replace comma with 4 spaces wasn't what I was looking for because each word has a different length and it would end up with inconsistences in every row.

But after much thinking, this was what I came up with.

Feel free to use/improve it!

public static function align($array)
{
    $column_sizes = array();
    foreach ($array as $a) {
        foreach ($a as $sub_key => $sub_val) {
            if (isset($column_sizes[$sub_key])) {
                if ($column_sizes[$sub_key] < strlen($sub_val)) {
                    $column_sizes[$sub_key] = strlen($sub_val);
                }
            } else {
                $column_sizes[$sub_key] = strlen($sub_val);
            }
        }
    }

    $result = '';
    foreach ($array as $a) {
        foreach ($a as $sub_key => $sub_val) {
            $result .= str_pad($sub_val, $column_sizes[$sub_key]+2, ' ', STR_PAD_RIGHT);
        }
        $result .= PHP_EOL;
    }

    return $result;

The first for-loop finds out what the largest string length for each column is. The second for-loop uses this data to add the correct amount of spaces at the end of each word so that each column is aligned cleanly.

Last updated 1 year ago.
0

Just out of curiosity, what is the use case for this? It seems like in most cases it would make more sense to do it with HTML tables if it's in a view. Or if you're writing an API, keep it as CSV or JSON.

Last updated 1 year ago.
0

I was doing some integration with Slack. Users can make data queries on Slack. I tried to use tabs at first but the app converts it to spaces, which results in uneven alignment. The data originates from a database but I used CSV just to keep the question simple.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

wyred wyred Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.