phpstringtext-extractiondelimited

Extract version number from a delimited string


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?


Solution

  • 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