为了账号安全,请及时绑定邮箱和手机立即绑定

检查数字是否在一组指定的数字之间?

检查数字是否在一组指定的数字之间?

PHP
POPMUISE 2022-10-09 17:18:45
这段代码:$url = ('example.url');function Feed($url) {  $feed = simplexml_load_file($url) or die('Cannot Connect');  $result = array();  foreach ($feed->channel->item as $content) {      array_push($result, $content);  }  return $result;}饲料.php ^^require_once 'feed.php';    $title = '123';    foreach(Feed('example.url') as $f ) {        if (strpos($f->title, $title) !== false)            echo $f->children('georss', true)->children('gml', true)->children('gml', true). "<br>";    }输出:59.838314 6.86571759.662615 10.56911059.709940 10.690916如何检查数字是否在我指定的数字之间?例子:59.68000和之间的经度59.850005.510000和之间的纬度8.750000它会输出59.838314 6.865717编辑:显示如果尝试过:    foreach(Feed('example') as $f ) {      if (strpos($f->title, $title) !== false)        if ($f->children('georss', true)->children('gml', true)->children('gml', true) >= 59.68000 && $f->children('georss', true)->children('gml', true)->children('gml', true) <= 59.85000) {          echo $f->children('georss', true)->children('gml', true)->children('gml', true). "<br>";        }    }在这里,我只检查经度。我想检查两者。任何帮助是极大的赞赏!
查看完整描述

2 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

在 PHP 中比较浮点值可能会有问题。如果这是您所要求的,您可以使用 bccomp() 来比较浮点值。


$a = '59.68000';

$b = '59.85000';

echo between($a, $b, '59.68001') . "\n"; // in range

echo between($a, $b, '59.68000') . "\n"; // exact match

echo between($a, $b, '59.88000') . "\n"; // higher than range

echo between($a, $b, '59.67000') . "\n"; // lower than range


function between($lower, $upper, $value)

{

    // Value is greater than lower bound of range

    $check1 = bccomp($value, $lower, 6) >= 0;

    // Value is less than upper bounds of range

    $check2 = bccomp($upper, $value, 6) >= 0;

    if ( $check1 && $check2 ) {

        return true;

    }

    return false;

}

使用功能:


foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=601') as $f ) {

    if (strpos($f->title, $title) !== false) {

        $long = $f->children('georss', true)->children('gml', true)->children('gml', true);

        $lat = $f->lattitude_value(); // Replace me!!

        if ( between('59.68000', '59.85000', $long) && between('5.510000', '8.750000', $lat) ) {

            echo $long . ' ' . $lat . "<br>";

        }

    }

}


查看完整回答
反对 回复 2022-10-09
?
catspeake

TA贡献1111条经验 获得超0个赞

我到处玩,发现这是解决我的问题的最佳方法:


    foreach(Feed($url) as $f ) {

      if (strpos($f->title, $title) !== false){

        $pos = $f->children('georss', true)->children('gml', true)->children('gml', true);

        list($number1, $number2) = explode(' ', $pos);

          if ($number1 > 50.00000 && $number1 < 71.00000){

            if ($number2 > 4.510000 && $number2 < 10.750000){

             echo $f->children('georss', true)->children('gml', true)->children('gml', true). "<br>";

            }

          }

        }

      }


查看完整回答
反对 回复 2022-10-09
  • 2 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信