如何在PHP中垂直回显文本行?
发布时间:2020-08-03 00:15:05 所属栏目:PHP 来源:互联网
导读:我在某个地方遇到这个问题,我想知道如何使用 PHP解决这个问题.给出这个文字: $str = PHP is awidely-usedgeneral-purposeserver sidescriptinglanguage; 如何垂直回显文字如下: g e n e w r
|
我在某个地方遇到这个问题,我想知道如何使用 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
这会修复其他答案的输出,这是不正确的(现在已经修复). (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
