Reputation: 299
I wish to extract:
8.4
from a PHP string ASD - 8.4 - iOS - P-CT
and
8.3.1
from ASD - 8.3.1 - Android - P-QL
.
Tried using floatval($var)
but it does not work all the times - for example, if I want to use it for both of my use cases. Are there any other ways to fetch such custom numbers from the PHP string?
Upvotes: 1
Views: 44
Reputation: 21489
Use regex to select target digit from string. Run your regex by php preg_match()
.
$str = "ASD - 8.4 - iOS - P-CT";
preg_match("/[\d.]+/", $str, $matches);
echo $matches[0];
Check code result in demo
Upvotes: 2
Reputation: 53626
explode() is probably your best bet here. Set your delimiter to -
and you'll get back an array of the four parts:
$str = 'ASD - 8.4 - iOS - P-CT';
$parts = explode(' - ', $str);
print_r($parts);
Output:
Array
(
[0] => ASD
[1] => 8.4
[2] => iOS
[3] => P-CT
)
Upvotes: 3