繁体   English   中英

休息不能正常工作

[英]Break doesn't work properly

foreach($item_cost as $node) {
            if($node->textContent != "$0" || $node->textContent != "$0.00" || $node->textContent != "S$0" || $node->textContent != "S$0.00" ){
                $price = $node->textContent;
                break;
            }
        }

我试图让它跳过0.00并抓住第一个值,例如17.50

我还是要0.00

你的二元运算符应该是&&而不是|| ,因为$node->textContent不能等于任何给定的字符串值。

if($node->textContent != "$0" && $node->textContent != "$0.00" && $node->textContent != "S$0" && $node->textContent != "S$0.00" ){

或者,您可以考虑使用正则表达式来匹配以美元或新加坡元计价零美元的东西:

if (!preg_match('/^S?\$0(\.00)?$/', $node->textContent)) {
    $price = $node->textContent;
    break;
}

或者,使用带有一组固定值的in_array()进行匹配。

尝试将if子句更改为:

foreach($item_cost as $node) {
  if (!in_array($node->textContent, array("$0","$0.00","S$0","S$0.00"))) {
    $price = $node->textContent;
    break;
  }
}

更容易阅读和更好地工作。

如果您需要所有价格(不仅仅是第一个),请使用它:

$prices = array();

foreach($item_cost as $node) {
  if (!in_array($node->textContent, array("$0","$0.00","S$0","S$0.00"))) {
     $prices[] = $node->textContent;
  }
}

现在$prices数组包含所有非空值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM