Separate the value from the string

4

Colleagues.

When calculating cart shipping, it is returning as follows:

Iwonder,howdoIgetonlythevalue24.90?Well,Imustaddtothevalueofthecart.Seethecodesbelow:

PHP

$parametros=http_build_query($parametros);$url='http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx';$curl=curl_init($url.'?'.$parametros);curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);$dados=curl_exec($curl);$dados=simplexml_load_string($dados);foreach($dados->cServicoas$linhas){if($linhas->Erro==0){echostr_replace(",",".",$linhas->Valor) .'</br>';
            echo "<strong>Prazo de entrega:</strong> ".$linhas->PrazoEntrega. "Dias </br>";
        }else {
            echo $linhas->MsgErro;
        }

JQUERY

ajax1.onreadystatechange = function() {
        if (ajax1.readyState == 4) {
           document.getElementById("result").innerHTML = ajax1.responseText;
           valorFrete = document.getElementById("result").innerHTML;

           vv = document.getElementById("total").innerHTML + valorFrete;
           valorTotal = document.getElementById("total").innerHTML = vv.toFixed(2);

        } else {
            document.getElementById("result").innerHTML = "Aguarde, calculando...";
        }
    }
    
asked by anonymous 17.03.2017 / 19:50

2 answers

7

Do this on the client side (in js), you can simply do:

responseText = '24.95, <strong> Outro texto, </strong>'; // faz de conta
valorFrete = parseFloat(responseText)
alert(valorFrete);

In this way you already have the numerical value, ready for mathematical operations.

Note that this only works if you are sure that the first character will always be a digit.

    
17.03.2017 / 19:53
4

You can break the string with a comma in this case, and get the first index:

let texto = '24.95, <strong> Outro texto, </strong>';
let valor = texto.split(',');
console.log(valor[0]);
    
17.03.2017 / 19:53