Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo lines of text vertically in PHP?

Tags:

text

php

I got this problem somewhere, and I want to know how to solve this using PHP. Given this text:

$str = '
PHP is a
widely-used

general-purpose
server side

scripting
language
';

How to echo the text vertically like the given below:

      g        
      e        
      n        
      e        
  w   r s      
  i   a e      
  d   l r   s  
P e   - v   c l
H l   p e   r a
P y   u r   i n
  -   r     p g
i u   p s   t u
s s   o i   i a
  e   s d   n g
a d   e e   g e

I will select the simpler and more elegant code as the answer.

like image 394
flowfree Avatar asked May 28 '12 12:05

flowfree


3 Answers

As others have already demonstrated, array_map is able to do the flip over which is basically the main problem you need to solve. The rest is how you arrange the code. I think your version is very good because it's easy to understand.

If you're looking more to some other extreme, handle with care:

$str = 'PHP is a
widely-used

general-purpose
server side

scripting
language';
    
$eol = "\n";
$turned = function($str) use ($eol) {
    $length = max(
        array_map(
            'strlen',
            $lines = explode($eol, trim($str))
        )
    );
    $each = function($a, $s) use ($length) {
        $a[] = str_split(
            sprintf("%' {$length}s", $s)
        );
        return $a;
    };
    return implode(
        $eol,
        array_map(
            function($v) {
                return implode(' ', $v);
            },
            call_user_func_array(
                'array_map',
                array_reduce($lines, $each, array(NULL))
            )
        )
    );
};
echo $turned($str), $eol;

Gives you:

      g        
      e        
      n        
      e        
  w   r s      
  i   a e      
  d   l r   s  
P e   - v   c l
H l   p e   r a
P y   u r   i n
  -   r     p g
i u   p s   t u
s s   o i   i a
  e   s d   n g
a d   e e   g e

This fixes the output of the other answer, which was incorrect (now fixed).

like image 156
hakre Avatar answered Nov 14 '22 01:11

hakre


The code below will print $str vertically.

$lines = preg_split("/\r\n/", trim($str));
$nl    = count($lines);
$len   = max(array_map('strlen', $lines));

foreach ($lines as $k => $line) {
  $lines[$k] = str_pad($line, $len, ' ', STR_PAD_LEFT);
}

for ($i = 0; $i < $len; $i++) {
  for ($j = 0; $j < $nl; $j++) {
    echo $lines[$j][$i].($j == $nl-1 ? "\n" : " ");
  }
}
like image 26
flowfree Avatar answered Nov 14 '22 00:11

flowfree


I took some of the code from @bsdnoobz and simplified it. I'm using \n as a line break because I work on a Mac and \r\n doean work here.

$lines = preg_split("/\n/", trim($str));
$len   = max(array_map('strlen', $lines));
$rows  = array_fill(0,$len,'');
foreach ($lines as $k => $line) {
    foreach (str_split(str_pad($line, $len, ' ', STR_PAD_LEFT)) as $row => $char){
          $rows[$row] .= $char;
    }
}
echo implode("\n",$rows)."\n";
like image 3
Ateszki Avatar answered Nov 14 '22 01:11

Ateszki