Php中的值格式删除caracteres

时间:2017-06-05 11:46:23

标签: php

我有这个值:

“24,7 km”

我想删除“Km”并且只获得24,7作为浮点数!有人知道我怎么做到这一点?非常感谢提前

这里我的代码是实际的:

Route::get('/test', function () {

    $origin = '155 avenue franklin roosevelt 11000 Carcassonne';

    $destination = '36 avenue André Chenier 11300 Limoux';

    $response = \GoogleMaps::load('directions')
        ->setParam([
            'origin'          => $origin,
            'destination'     => $destination,
            'mode' => 'driving' ,
            'language' => 'fr',

        ])->get();



   $parsed_json = (json_decode($response));


   $distance = $parsed_json->{'routes'}[0]->{'legs'}[0]->{'distance'}->{'text'};

   dd($distance);

更新工作:

  $parsed_json = (json_decode($response));


        $distance = $parsed_json->{'routes'}[0]->{'legs'}[0]->{'distance'}->{'text'};


        $a = $distance;
        $b = str_replace(" km",'',$a);
        $c = str_replace(",",'.',$b);

        dd($c);

1 个答案:

答案 0 :(得分:1)

$a = "24,7 km";
$b = str_replace(" km",'',$a);
$c = str_replace(",",'.',$b);