2016-02-17 08:50
浏览 107

PHP for循环中的换行符

There have been a bunch of questions like this already, but none of the answers seem to help me. I would like to have a line-break after each output from a loop. I am using double-quoted strings, like I read here, as well as using HTML (because I want the browser to recognize the line-breaks too) as I read here.

It does create a line-break, however only below the complete output.

I cannot manage to create a line-break between the outputs of the loop. Basically I get a block of text and then a linebreak.

Here is the loop I am using:

$target_url = "http://www.buzzfeed.com/trending?country=en-us";
$html = new simple_html_dom();


$posts = $html->find('ul[class=list--numbered trending-posts trending-posts-    now]');
$limit = 10;
$limit = count($posts) < $limit ? count($posts) : $limit;
for($i=0; $i < $limit; $i++){
  $post = $posts[$i];
  $post->find('div[class=trending-post-text]',0)->outertext = "";
  echo strip_tags($post, '<p><a>') . "<br/>

I've also tried " " and a bunch of variations, as well as the nl2br() function. I believe the PHP_EOL command is meant only for the command line, from what I've researched.

I'm an absolute beginner with PHP, so I am probably missing something simple, but I can't figure it out.

EDIT: Here is what it prints: http://globalsocialnews.com/crawler/test8.php

I also included the complete code in case that helps.

图片转代码服务由CSDN问答提供 功能建议

已经有很多这样的问题,但没有一个答案似乎对我有帮助。 我希望在循环的每个输出后都有一个换行符。 我使用的是双引号字符串,就像我在这里读到的那样, 以及使用HTML(因为我希望浏览器也能识别换行符)我在这里读到


我无法创建一个 循环输出之间的换行符。 基本上我得到一个文本块然后换行。


 <  code>&lt;?php 
 $ target_url =“http://www.buzzfeed.com/trending?country=en-us";
$html = new simple_html_dom()  ; 
 $ html-&gt; load_file($ target_url); 
 $ posts = $ html-&gt; find('ul [list = list-numbered trending-posts trending-posts- now]')  ; 
 $ limit = 10; 
 $ limit = count($ posts)&lt;  $ limit?  count($ posts):$ limit; 
for($ i = 0; $ i&lt; $ limit; $ i ++){
 $ post = $ posts [$ i]; 
 $ post-&gt; find('  div [class = trending-post-text]',0) - &gt; outertext =“”; 
 echo strip_tags($ post,'&lt; p&gt;&lt; a&gt;')。  “&lt; br /&gt; 

我还尝试了“ ”和 一系列变体,以及 nl2br()函数。 我相信PHP_EOL命令仅适用于我所研究的命令行。

我是PHP的绝对初学者,所以我可能错过了一些简单的东西,但我 无法理解。

编辑: 这是它打印的内容: http://globalsocialnews.com/crawler/test8.php


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqhmtpuy94946
    dqhmtpuy94946 2016-02-17 09:12

    You can do line break using css implementation ... like:

    .rcorners1 a {
        display: block;
        margin-bottom: 10px;//for show extra margin between each line...  

    Edit: Use margin css attribute for extra margin...

    please try implementing this...

    点赞 评论
  • douxiong2999
    douxiong2999 2016-02-17 09:34

    Better Use

    echo "</br>";
    点赞 评论