1

I am trying to create a newline character in my foreach loop. After the URL I am trying to create a newline and then have a "------" between the title and content. After this there is supposed to be a space between the next URL. I have the space but I can't seem to get the line break. When I echo "\t" or "\n" in the loop nothing happens. When I use HTML in the loop it won't work, gives too many spaces.

for($i=0; $i < 4 ;$i++)
{
foreach ($json as $URL)
    {
        echo $URL ['results'][$i]['url'];
        echo "---------------";
        foreach ($json as $TITLE)
        {
              echo $TITLE ['results'][$i]['title'];
              echo "--------";
        }
        foreach ($json as $content)
              echo $content['results'][$i]['content'];
        echo "---------------- ";
    }
}

Is there a function in php besides "\n" or another way of manipulating the HTML to insert the line break?

TIA

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
Daniel o keeffe
  • 51
  • 1
  • 2
  • 8
  • 1
    you can use echo '
    '
    – Bharu Jul 11 '13 at 11:14
  • 1
    Try a light-fix `echo $TITLE ['results'][$i]['title'] . " ";` Non-breakable space wont give much space taht you want to avoid. – Ignat B. Jul 11 '13 at 11:15
  • 2
    [http://stackoverflow.com/questions/2531969/print-newline-in-php-in-single-quotes](http://stackoverflow.com/questions/2531969/print-newline-in-php-in-single-quotes) – Lan Jul 11 '13 at 11:15
  • 1
    why dont you just use
    or nl2br()
    – Ali Almoullim Jul 11 '13 at 11:16
  • When I use echo"
    "; in the first foreach loop it gives me the url, 3 spaces and then the title and content. I'm thinking it must have something to do with the foreach loop itself
    – Daniel o keeffe Jul 11 '13 at 11:31
  • Cheers Ignat, I just used echo Surl['results'][['$i']['url']."
    " which works perfectly, and gives me an idea on another problem as to how to hyperlink the urls. Thanks guys.
    – Daniel o keeffe Jul 11 '13 at 11:36

7 Answers7

4

There are two things you can do:

1: Use <br> as line break. If you use that, you should also consider using <hr> to insert the horizontal line.

some text<br>
<hr>
some other text<br>

2: Use <pre> and </pre> tags around your text to output preformatted text. You can then use \n and \t to format your text.

<pre>
some text\n
-------\n
some other text\n
</pre>
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
3

This might the one you are searching for

for($i=0; $i < 4 ;$i++)
{
foreach ($json as $URL)
    {
        echo $URL ['results'][$i]['url'];
    echo "<br/>";        
    echo "---------------";
        foreach ($json as $TITLE)
        {
              echo $TITLE ['results'][$i]['title'];
              echo "--------";
        }
        foreach ($json as $content)
              echo $content['results'][$i]['content'];
        echo "---------------- ";
    }
}
San
  • 632
  • 4
  • 13
2

\n doesn't work in HTML. If you view source of the document you will however see the line breaks. Use html markup like

Test line break<br />
<p>paragraph</p>
<p>paragraph</p>
JimL
  • 2,501
  • 1
  • 19
  • 19
1

Simply add <br /> to echo statements. as in html we use this for line breaks.

echo "Content"."<br />";

This would give line breaks in HTML format.

1

use

 echo "<br />";

instead of

 echo "---------------- ";
chirag ode
  • 950
  • 7
  • 15
1

Use echo '
';

<?php
    echo "first line.<br />Second line.';
?>

or nl2br() function,

<?php
    echo nl2br("first line.\nSecond line.");
?>
way2vin
  • 2,411
  • 1
  • 20
  • 15
0

You can consider echo "<hr /> ";

Rahul H
  • 89
  • 4