【问题标题】:Should I use explode, split, and/or trim; and how?我应该使用爆炸、拆分和/或修剪吗?如何?
【发布时间】:2011-12-21 00:09:35
【问题描述】:

我想从中获得一些价值:

$bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';

我的目标是将四个数字中的每一个都列为自己的变量:

$swla = -34.41859810454894;
$swlo = 150.5594567718506;
$nela = -34.375112955999064;
$nelo = 150.74124617004395;

这是我糟糕的破解尝试:

$pieces = explode("),", $bounds);
var_dump($pieces); //array(2) { [0]=> string(39) "((-34.41859810454894, 150.5594567718506" [1]=> string(43) " (-34.375112955999064, 150.74124617004395))" } 
echo '<br />';

$sw = trim(substr($pieces[0], 2));
echo $sw .'<br />'; //-34.41859810454894, 150.5594567718506
$ne = trim(substr($pieces[1], 2), " ) ");
echo $ne.'<br />'; //-34.375112955999064, 150.74124617004395

$sw = explode(",", $sw);
var_dump($sw); //array(2) { [0]=> string(18) "-34.41859810454894" [1]=> string(18) " 150.5594567718506" } 
echo '<br />';
$ne = explode(",", $ne); //array(2) { [0]=> string(19) "-34.375112955999064" [1]=> string(19) " 150.74124617004395" } 
var_dump($ne);
echo '<br />';

谢谢

【问题讨论】:

    标签: php google-maps trim explode


    【解决方案1】:

    最短的方法大概是preg_match_all

    preg_match_all('/[0-9.-]+/', $bounds, $matches);
    list($swla, $swlo, $nela, $nelo) = $matches[0];
    

    如果你想要浮动,那么你可以使用sscanf。但是,这些数字太精确了,无法正确拟合 PHP 双精度数,这样做会损失一些精度。

    【讨论】:

      【解决方案2】:

      只是为了表明你可以使用 sscanf()

      $bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
      
      sscanf($bounds,'((%[-0-9.], %[-0-9.]), (%[-0-9.], %[-0-9.]))',$swla,$swlo,$nela,$nelo);
      
      echo $swla,PHP_EOL;
      echo $swlo,PHP_EOL;
      echo $nela,PHP_EOL;
      echo $nelo,PHP_EOL;
      

      给予

      -34.41859810454894 
      150.5594567718506 
      -34.375112955999064 
      150.74124617004395
      

      【讨论】:

        【解决方案3】:

        您可以使用sscanf()

        sscanf($bounds, "((%f, %f), (%f, %f))", $swla, $swlo, $nela, $nelo);
        

        这会将您的坐标转换为floats。您可以了解有关格式的更多信息here

        【讨论】:

          【解决方案4】:

          我个人会这样做:-

          $bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
          $bounds = str_ireplace(array('(', ')'), '', $bounds);
          $bounds = explode(',', $bounds);
          foreach($bounds as $key => $bound){
              $bounds[$key] = floatval($bound);
          }
          var_dump($bounds);
          

          输出:-

          数组
          0 => 浮动 -34.418598104549
          1 => 浮动 150.55945677185
          2 => 浮动 -34.375112955999
          3 => 浮动 150.74124617004

          我不知道你需要多少精度,但是这种方法确实会损失一些。

          【讨论】:

            【解决方案5】:

            我喜欢 preg_match 版本,但根据您的使用情况,您当然也可以使用 explode 将它们加载到数组中。

             $bounds = '((-34.41859810454894, 150.5594567718506), (-34.375112955999064, 150.74124617004395))';
             $tempArray = explode(',', $bounds); 
            
             //Gives you $tempArray[0] = ((-34.41859810454894
             //and $tempArray[1] =  150.5594567718506)
             //and $tempArray[2] = (-34.375112955999064
             //and $tempArray[3] =  150.74124617004395))
            
             $latLongArray = array();
             foreach($tempArray as $latLong)
             {
                 $tempValue = str_replace('(', '', $tempValue);
                 $tempValue = str_replace(')', '', $tempValue);
                 $latLongArray[] = trim($tempValue);
             }
            

            然后,您可以将 LatLong 数组推送到带有变量名称/等的关联数组中。这实际上只是您是否要使用正则表达式或输入的模板化程度的问题。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-03-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-26
              相关资源
              最近更新 更多