如何在PHP中垂直回显文本行?

我在某个地方遇到这个问题,我想知道如何使用 PHP解决这个问题.给出这个文字
$str = '
PHP is a
widely-used

general-purpose
server side

scripting
language
';

如何垂直回显文字如下:

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

我会选择更简单和更优雅的代码作为答案.

正如其他人已经展示的那样,array_map能够做到这一点,这基本上是你需要解决的主要问题.其余的是你如何安排代码.我觉得你的版本很好,因为它很容易理解.

如果你想要更多的其他极限,请小心处理:

$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;

给你:

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

这会修复其他答案的输出,这是不正确的(现在已经修复).

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...