Breaking a string and display in new line

$str= “I am enjoying life”;

I want to print above string as:

I
am
enjoying
life

How can I do it?

There are many, many ways, check the manual for string functions. :wink:

html


<?php
$string = 'I am enjoying life';
echo implode(
    '<br />',
    explode(
        ' ',
        $string
    )
);
?>

plain text/cli


<?php
$string = 'I am enjoying life';
echo implode(
    "\
",
    explode(
        ' ',
        $string
    )
);
?>

Or str_replace(’ ', "
<br />", $text);


<?php
$str = "I am enjoying life";
$str = str_replace(" ", "<br />", $str);
echo $str;
?>

As said above, simply replaces a space with a new line. " " to <br />.