I have a textile that looks like this:
Most favourite book: 011 A world apart
4 favourite books: 300, 298, 223, 122
Mad's trial, Go on, Let's Pray, What a life!
It is well spaced and neat. I am reading this file in php and using echo to print its contents. Echo messes up everything and prints everything in the file in one line. How can I add spaces and make the output look nicer? I am new to php and not very familiar.
<div align="center"><h4 style="line-height:150%;"><?php echo $book; ?></h5></div>
$book
here is the file.
Just enclose your text with <pre>
tags:
$str = "Most favourite book: 011 A world apart
4 favourite books: 300, 298, 223, 122
Mad's trial, Go on, Let's Pray, What a life!";
echo "<pre>". $str . "</pre>";
It's not that echo
function messes up with your text. It is the browser that displays your text without extra spaces. To avoid this you'll have to use <pre>
html tag. Tag reference here
echo '<pre>'. $your_string . '</pre>';
Or,
<pre> <?php echo $your_string; ?> </pre>
Your code must be like this:
<div align="center">
<h4 style="line-height:150%;">
<pre>
<?php echo $book; ?>
</per>
</h4>
</div>
Depending on how you want to show the text, enclosing it between <pre>
tags might not be enough. Take a look at php's nl2br
function, which converts newline characters ( on Windows,
on Unix or similar) to
<br>
tags. See the function reference here: http://php.net/manual/en/function.nl2br.php.
In your example:
echo nl2br($str);