如何在单个回声中组合多个 foreach?
how to combine multiple foreach in single echo?
<?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 />'; ?>
现在是多个回声的结果..我希望所有这些结果都在一个回声中,然后我会将结果存储到我的数据库中..
在 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; ?>
使用变量,并附加值。
<?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;
?>
我不确定,但也许是这个?:
<?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
}
}
创建一个 php 数组并将每个 echo 的结果放入该数组中,然后 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);
}
?>
对其他人执行此操作,然后按照您的意愿使用您的数组
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'));
这个怎么样
<?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 />'; ?>
现在是多个回声的结果..我希望所有这些结果都在一个回声中,然后我会将结果存储到我的数据库中..
在 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; ?>
使用变量,并附加值。
<?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;
?>
我不确定,但也许是这个?:
<?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
}
}
创建一个 php 数组并将每个 echo 的结果放入该数组中,然后 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);
}
?>
对其他人执行此操作,然后按照您的意愿使用您的数组
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'));
这个怎么样