Php 是否将文本仅放在循环的第一个结果之后?
目前我有这个循环Php 是否将文本仅放在循环的第一个结果之后?,php,wordpress,Php,Wordpress,目前我有这个循环 foreach ($html2->find('.entry-content img') as $image) { $path = $image->src; $start = '<img src="'.$path.'" style="height: auto; width: 100%;margin-bottom: 3px;">'; print htmlspecialchars($start); print '<br&g
foreach ($html2->find('.entry-content img') as $image) {
$path = $image->src;
$start = '<img src="'.$path.'" style="height: auto; width: 100%;margin-bottom: 3px;">';
print htmlspecialchars($start); print '<br>';
}
foreach($html2->find('.entry content img')作为$image){
$path=$image->src;
$start='';
打印htmlspecialchars($start);打印“
”;
}
这是从一个网站上用simple\u html\u dom.php抓取一些图像,这部分工作非常好。但是,我需要使它只在foreach循环中的第一个结果之后返回WordPress
标记
我如何才能做到这一点
谢谢您可以尝试检查它是否是循环中的第一个,如下所示:
$first = TRUE;
foreach ($html2->find('.entry-content img') as $image) {
$path = $image->src;
$start = '<img src="'.$path.'" style="height: auto; width: 100%;margin-bottom: 3px;">';
if($first){
$start .= "<!--more-->";
$first = FALSE;
}
print htmlspecialchars($start); print '<br>';
}
$first=TRUE;
foreach($html2->find('.entry content img')作为$image){
$path=$image->src;
$start='';
如果($第一){
$start.=“”;
$first=FALSE;
}
打印htmlspecialchars($start);打印“
”;
}
您可以尝试检查它是否是循环中的第一个,如下所示:
$first = TRUE;
foreach ($html2->find('.entry-content img') as $image) {
$path = $image->src;
$start = '<img src="'.$path.'" style="height: auto; width: 100%;margin-bottom: 3px;">';
if($first){
$start .= "<!--more-->";
$first = FALSE;
}
print htmlspecialchars($start); print '<br>';
}
$first=TRUE;
foreach($html2->find('.entry content img')作为$image){
$path=$image->src;
$start='';
如果($第一){
$start.=“”;
$first=FALSE;
}
打印htmlspecialchars($start);打印“
”;
}
一个简单的if
就可以完成这项工作
$cond = false;
foreach($html2->find('.entry-content img') as $image) {
//Always done
if (!$cond) {
//Will be called only during the first iterration
$cond = true;
}
else if ($cond){
//This part will always be executed after the first iterration
}
}
一个简单的
if
就可以完成这项工作
$cond = false;
foreach($html2->find('.entry-content img') as $image) {
//Always done
if (!$cond) {
//Will be called only during the first iterration
$cond = true;
}
else if ($cond){
//This part will always be executed after the first iterration
}
}