如何在单个回声中组合多个foreach?

时间:2015-02-06 10:00:44

标签: php html parsing dom foreach

<?php
include 'simple_html_dom.php';  
// Create DOM from URL 
$html = file_get_html('http://some site. com/'); ?>

<?php
foreach($html->find('a.cellMainLink') as $title) 
echo '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
    <td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>'; 
?>

<?php 
    foreach($html->find('td.nobr') as $size)
    echo '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
    <td>'.$size.'</td></tr></table></div>'; 
    ?>

<?php 
foreach($html->find('td.green') as $seeds) 
echo '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr> <td>'.$seeds.'</td></tr></table></div>'; ?>

<?php 
foreach($html->find('td.red') as $leechs) 
echo '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr> <td>'.$leechs.'</td></tr></table></div>'; ?>

<?php 
foreach($html->find('strong a[1]') as $category) 
echo '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr><td>'.$category->plaintext.'</td></tr></table></div>'; ?>

<?php 
foreach($html->find('a[class=idownload icon16]') as $down) 
echo '<div style="font-size:12px">'.$down->href.'</div><br />'; ?>

现在结果是多个回声..我希望所有这些结果都在一个回声中然后我将结果存储到我的数据库中..

5 个答案:

答案 0 :(得分:2)

在php变量中使用.=。它是连接赋值运算符。

<?php
include 'simple_html_dom.php';  // Create DOM from URL 
$html = file_get_html('http://some site. com/');
$what_you_echo = "";
?>

<?php
foreach($html->find('a.cellMainLink') as $title) 
 $what_you_echo .= '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
    <td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>'; 
?>

<?php 
    foreach($html->find('td.nobr') as $size)
    $what_you_echo .= '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
    <td>'.$size.'</td></tr></table></div>'; 
    ?>

<?php 
foreach($html->find('td.green') as $seeds) 
$what_you_echo .=  '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr> <td>'.$seeds.'</td></tr></table></div>'; ?>

<?php 
foreach($html->find('td.red') as $leechs) 
$what_you_echo .=  '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr> <td>'.$leechs.'</td></tr></table></div>'; ?>

<?php 
foreach($html->find('strong a[1]') as $category) 
$what_you_echo .=  '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr><td>'.$category->plaintext.'</td></tr></table></div>'; ?>

<?php 
foreach($html->find('a[class=idownload icon16]') as $down) 
$what_you_echo .=  '<div style="font-size:12px">'.$down->href.'</div><br />'; ?>

<?php echo $what_you_echo; ?>

您可以通过删除大量<?php

来设置代码的样式
<?php
include 'simple_html_dom.php';  // Create DOM from URL
$html = file_get_html('http://some site. com/');
$what_you_echo = "";

foreach($html->find('a.cellMainLink') as $title) 
$what_you_echo .= '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr><td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>'; 

foreach($html->find('td.nobr') as $size)
$what_you_echo .= '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr><td>'.$size.'</td></tr></table></div>'; 

foreach($html->find('td.green') as $seeds) 
$what_you_echo .=  '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr> <td>'.$seeds.'</td></tr></table></div>';

foreach($html->find('td.red') as $leechs) 
$what_you_echo .=  '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr> <td>'.$leechs.'</td></tr></table></div>';

foreach($html->find('strong a[1]') as $category) 
$what_you_echo .=  '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr><td>'.$category->plaintext.'</td></tr></table></div>';

foreach($html->find('a[class=idownload icon16]') as $down) 
$what_you_echo .=  '<div style="font-size:12px">'.$down->href.'</div><br />';

echo $what_you_echo; ?>

答案 1 :(得分:0)

使用变量,并附加值。

<?php
$out = '';
include 'simple_html_dom.php'; 
// Create DOM from URL
$html = file_get_html('http://some site. com/');
foreach ($html->find('a.cellMainLink') as $title) {
    $out .= '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
    <td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
}
foreach ($html->find('td.nobr') as $size) {
    $out .= '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
    <td>'.$size.'</td></tr></table></div>';
}
foreach ($html->find('td.green') as $seeds) {
    $out .= '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr>
    <td>'.$seeds.'</td></tr></table></div>';
}
foreach ($html->find('td.red') as $leechs) {
    $out .= '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr>
    <td>'.$leechs.'</td></tr></table></div>';
}
foreach ($html->find('strong a[1]') as $category) {
    $out .= '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr>
    <td>'.$category->plaintext.'</td></tr></table></div>';
}
foreach ($html->find('a[class=idownload icon16]') as $down) {
    $out .= '<div style="font-size:12px">'.$down->href.'</div><br />';
}
echo $out;
?>

答案 2 :(得分:0)

我不确定,但也许这个?:

<?php
include 'simple_html_dom.php';  // Create DOM from URL
$html = file_get_html('http://some site. com/');

$el_arr['a.cellMainLink'] = $html->find('a.cellMainLink');
$el_arr['td.nobr'] = $html->find('td.nobr');
$el_arr['td.green'] = $html->find('td.green');
$el_arr['td.red'] = $html->find('td.red');
$el_arr['strong [1]'] = $html->find('strong [1]');
$el_arr['a[class=idownload icon16]'] = $html->find('a[class=idownload icon16]');
foreach($el_arr as $key=>$el)
{
 foreach($el as $node)
 {
  //echo your content accordingly here using $key and $node
  //$key represent the type of element and $node represent a node of that type from html
 }
}

答案 3 :(得分:0)

创建一个php数组并将每个echo的结果放在这个数组中,然后回显数组示例:

 <?php
    $results=array[];
    $our_var='';
    foreach($html->find('a.cellMainLink') as $title)
{ 
    $our_var='<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
        <td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
    array_push($results,$our_var); 
}
    ?>

根据您的意愿对其他人进行操作

答案 4 :(得分:0)

include 'simple_html_dom.php';  // Create DOM from URL 
$html = file_get_html('http://some site. com/');
$what_you_echo = "";

foreach($html->find('a.cellMainLink') as $title) 
    echo  '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr><td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>'; 
    $size = current($html->find('td.nobr'));
    echo '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
    <td>'.$size.'</td></tr></table></div>'; 
    next($html->find('td.nobr'));

这个怎么样