我如何将其排成一行 [英] how do i sort this in one line

查看:63
本文介绍了我如何将其排成一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字符串:

  $te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';

  $text=strlen($te);
  if($text<=10)       
  { 
  echo '2';
  }
  if($text>=11) 
  {
  echo '4';
   }
  if($text>=19) 
  { 
  echo '6';
  }
  if($text>=29) 
  {
  echo '8'; 
  }
 if($text>=39) 
  { 
  echo '10'; 
  }

我可以在另一行代码中做到这一点吗?而不是太多(IF)?
或者,当$ text的字符非常大(如最多100个字符)时,我将如何实现我的预期输出

which other way can i do this in one line of code? Instead of too many (IF)? Or how will i achieve this my expected output when the characters for $text is very large like upto hundred in characters

在此先感谢

推荐答案

在开始解释之前,我将等一下我的(现在是第四次尝试)是否正确……

Before I bother with an explanation, I'll wait to hear if my (now fourth attempt) is correct...

代码:( 演示

$strings=[9=>'123456789',10=>'1234567890',
          11=>'12345678901',18=>'123456789012345678',
          19=>'1234567890123456789',
          28=>'1234567890123456789012345678',
          38=>'12345678901234567890123456789012345678'];
foreach($strings as $k=>$str){
    echo "$k => $str: ",($len=strlen($str))<11 ? 2 :2*(floor(($len+1)/10)+1),"\n";
}
//0 - 10 : 1 2
//11 - 18 : 2 4
//19 - 28 : 3 6
//29 - 38 : 4 8

输出:

9 => 123456789: 2
10 => 1234567890: 2
11 => 12345678901: 4
18 => 123456789012345678: 4
19 => 1234567890123456789: 6
28 => 1234567890123456789012345678: 6
38 => 12345678901234567890123456789012345678: 8

这篇关于我如何将其排成一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆