I am trying to show temperature on my website. I have a forecast in json that I get from api and there is this field that I need to get the temperature from:
"fcttext_metric":"A mainly sunny sky. High near 30 ° C. Winds SE at 10 to 15 km/h."
$str = 'A mainly sunny sky. High near 30 ° C. Winds SE at 10 to 15 km/h.';
$deg = filter_var($str, FILTER_SANITIZE_NUMBER_FLOAT);
$res = substr($deg, 0, 2);
print_r($res);
I would use this regex. It should match integer values before ° (°) sign. Like 30 or -30
preg_match('/(\-?\d+)\s*°/', $str, $matches);
echo $matches[1];