Reputation: 239
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
Reputation: 5668
$str = "Hello World";
$result = "";
for ($i = 0; $i < strlen($str); $i++) {
$result .= $str[$i] . "\n";
}
echo $result;
Upvotes: 0
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
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