0

I'm trying to only get the first 19.45 from this string `` Is there a way to grab everything in between ..."raw": AND ,"fmt... so i only get the number. Hope someone can help me...

UPDATE: found what I was looking for.
How to get a substring between two strings in PHP?

<?php 
$str = "targetMeanPrice":{"raw":19.45,"fmt":"19.45"}";

function GetBetween($var1='',$var2='',$pool){
$temp1 = strpos($pool,$var1)+strlen($var1);
$result = substr($pool,$temp1,strlen($pool));
$dd=strpos($result,$var2);
if($dd == 0){
    $dd = strlen($result);
}
return substr($result,0,$dd);
}

echo GetBetween('raw":',',"fmt', "$str" ); 
?>

output = 19.45

  • 4
    Is this the whole source string? It looks like an incomplete chunk of JSON -- which you'd be able to use `json_decode()` on. – Alex Howansky Nov 02 '17 at 19:41

3 Answers3

2
$decodedJSON = json_decode($yourJson);
$raw = $decodedJSON->targetMeanPrice->raw;

$raw will now contain the value of raw, which is 19.45

or

$decodedJSON = json_decode($yourJson,true);
$raw = $decodedJSON['targetMeanPrice']['raw']

source

acaputo
  • 190
  • 12
  • Regarding your "found" answer, I think it's a little silly to be doing string pos when you actually have a JSON object. Why not leverage that? – acaputo Nov 03 '17 at 00:30
0

Try this:

   <?php 

    $json = '{"targetMeanPrice":{"raw":19.45,"fmt":"19.45"}}';
    $json = json_decode($json);
    echo $json->targetMeanPrice->raw;

    ?>
0

You can use strpos(...) to find "raw": and second strpos(..) to find ,"fmt, than calculate length/difference from returned indexes by strpos(...) and use substr(...) to extract expected value

armo
  • 54
  • 3