【问题标题】:Linebreak after each character每个字符后的换行符
【发布时间】:2012-02-18 15:56:02
【问题描述】:

如果我想在每个字符后创建一个新的换行符,什么 PHP 代码最容易使用。 假设我有这个字符串“hello world!”。现在我不想做的是让它看起来像这样:

h

e

l

l

o

w

o

r

l

d

我应该使用什么样的代码。我现在只有换行代码,而不是字符之间的空格代码。提前致谢!

【问题讨论】:

  • 你实际上需要 2 个换行符,而不是一个

标签: php html character line-breaks


【解决方案1】:
echo implode("\n\n",str_split("Hello world"));

【讨论】:

    【解决方案2】:
    for($i = 0; $i<strlen($str); $i++)
    {
        $newstr .= substr ($str,$i,1).chr(YOURDESIREDCHAR);
    }
    

    您可以在此处查找 chr 的值:http://www.asciitable.com/

    对于换行符,您只需添加“\n”而不是 chr(...)

    【讨论】:

      【解决方案3】:

      如果输出是 HTML,只需在每个字符后添加一个中断。

      function splitChars( $str )
      {
          $out;
      
          for ( $i = 0; i < strlen( $str ); $i++ )
              $out .= $str{$i} . '<br />';
      
          return $out;
      }
      

      【讨论】:

        【解决方案4】:
        $str = "Hello World";
        $result = "";
        for ($i = 0; $i < strlen($str); $i++) {
             $result .= $str[$i] . "\n";
        }
        echo $result;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-09
          • 1970-01-01
          • 1970-01-01
          • 2012-06-10
          • 1970-01-01
          • 2014-10-16
          相关资源
          最近更新 更多