[Solved] I want to get “0.8” from my string using PHP


To get the last 3 characters:

$string = "2.5 lakh videos views 0.8";
$valSubstr = substr($string, -3);
echo $valSubstr;

To get the last “part” of the sentence. (this way you don’t have to care about the count of characters in your number)

$string = "2.5 lakh videos views 0.8";
$partsString = explode(' ', $string);
$valExplode = $partsString[count($partsString) - 1];
echo $valExplode;

Both methods are bad, because you can only hope your data is in the format you expect it to be. But if your data is always in the format you showed, it would work. You can add a regex to check for unwanted characters like a trailing “.”.

2

solved I want to get “0.8” from my string using PHP