ZyteX
ZyteX

Reputation: 239

Linebreak after each character

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!

Upvotes: 1

Views: 737

Answers (4)

Patrick Lorio
Patrick Lorio

Reputation: 5668

$str = "Hello World";
$result = "";
for ($i = 0; $i < strlen($str); $i++) {
     $result .= $str[$i] . "\n";
}
echo $result;

Upvotes: 0

m02ph3u5
m02ph3u5

Reputation: 3161

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

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157989

echo implode("\n\n",str_split("Hello world"));

Upvotes: 5

jwacalex
jwacalex

Reputation: 517

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(...)

Upvotes: 0

Related Questions