【问题标题】:Line Break in PHP for-loopPHP for循环中的换行符
【发布时间】:2016-05-28 20:48:57
【问题描述】:

已经有很多这样的问题,但似乎没有一个答案对我有帮助。我想在循环的每个输出之后都有一个换行符。我正在使用双引号字符串like I read here,以及使用 HTML(因为我希望浏览器也能识别换行符)as I read here

它确实会创建一个换行符,但仅在下面完整的输出。

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

这是我正在使用的循环:

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

$html->load_file($target_url);

$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/>\n";
}

我还尝试了"\r\n" 和一系列变体,以及nl2br() function。根据我的研究,我相信 PHP_EOL 命令仅适用于命令行。

我是 PHP 的绝对初学者,所以我可能遗漏了一些简单的东西,但我想不通。

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

我还包括了完整的代码以防万一。

【问题讨论】:

  • 你能展示一下它打印的内容吗?
  • 你能把输出粘贴到某处吗?
  • “它确实会创建一个换行符,但仅在完整输出下方。”&lt;br /&gt; 放在关闭 for 循环的 } 之后
  • 您的代码示例中有错误:Parse error: syntax error, unexpected '&lt;' in /home/jasperko/public_html/globalsocialnews.com/crawler/test8.php on line 51
  • 我都试过了,都没有成功。下面的 Ashish 有一个最终对我有用的答案

标签: php html loops for-loop


【解决方案1】:

您可以使用 css 实现进行换行... 喜欢:

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

    }

编辑:使用边距 css 属性获得额外边距...

请尝试实现这个...

【讨论】:

  • 这成功了!还有没有办法为此添加另一个换行符?
【解决方案2】:

更好的使用

echo "</br>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 2013-03-16
    • 1970-01-01
    • 2019-03-12
    • 2015-01-28
    • 2013-06-12
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多