【发布时间】:2017-06-29 00:16:06
【问题描述】:
我在 while 循环中的 foreach 中有一个 if。我正在另一个文本字符串中搜索一个字符串。
$base = 'someurl';
$url = $base;
while(1){
$json = json_decode(file_get_contents($url));
$more = $json->next_page;
$next = $json->next_page_number;
if($more){
$url = $base.'&page='.$next;
}else{
break;
}
foreach($json as $data){
$c = $data->text; // search in
$s = 'string';
$t = 'prefix1'.$s; // search for
$m = 'prefix2'.$s; // search for
if( (strpos($c, $t) || strpos($c, $m)) !== false ){
echo '<p>hello, i found one or more ($t and/or $m) of the search criteria in the text string $c .</p>';
break;
}
}
}
问题是我从 if 语句中得到了两次回显:(
我想要实现的是仅在 $c 中存在 $t 和/或 $m 时才使回显发生一次。
【问题讨论】:
-
据我所见,由于
while(1);,该行将无限期回显 -
@Jerodev - 是的,原始循环中有一个 if/break,我的错。查看更新的代码;)
标签: php if-statement foreach while-loop do-while