每个字符后换行

发布于 2025-01-06 16:52:20 字数 314 浏览 4 评论 0原文

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

小时

e

l

l

o

w

o

r

l

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

What PHP code would be easiest to use if I want to create a new linebreak after each character.
Lets say that I have this string "hello world!". Now what I wan't to do is to make it look like this:

h

e

l

l

o

w

o

r

l

d

!

What kind of a code should I use. I only now the linebreak codes and not a code for space between the characters. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荭秂 2025-01-13 16:52:20
echo implode("\n\n",str_split("Hello world"));
echo implode("\n\n",str_split("Hello world"));
软糖 2025-01-13 16:52:20
for($i = 0; $i<strlen($str); $i++)
{
    $newstr .= substr ($str,$i,1).chr(YOURDESIREDCHAR);
}

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

对于换行符你可以只添加“\n”而不是 chr(...)

for($i = 0; $i<strlen($str); $i++)
{
    $newstr .= substr ($str,$i,1).chr(YOURDESIREDCHAR);
}

You can lookup the value for chr here: http://www.asciitable.com/

For linebreak(s) you can just add "\n" instead of chr(...)

深巷少女 2025-01-13 16:52:20

如果输出为 HTML,只需在每个字符后添加一个分隔符即可。

function splitChars( $str )
{
    $out;

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

    return $out;
}

If the output shall be HTML simply add a break after each character.

function splitChars( $str )
{
    $out;

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

    return $out;
}
阪姬 2025-01-13 16:52:20
$str = "Hello World";
$result = "";
for ($i = 0; $i < strlen($str); $i++) {
     $result .= $str[$i] . "\n";
}
echo $result;
$str = "Hello World";
$result = "";
for ($i = 0; $i < strlen($str); $i++) {
     $result .= $str[$i] . "\n";
}
echo $result;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文