休息不能正常工作

时间:2013-12-11 02:10:09

标签: php html break

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

2 个答案:

答案 0 :(得分:1)

您的二元运算符应该是&&而不是||,因为$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()与一组固定的值进行匹配。

答案 1 :(得分:1)

尝试将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数组包含所有非空值。

相关问题