Pradeep shyam
Pradeep shyam

Reputation: 1292

Extract decimal or Integer from a string in php

Eg:

$str .= "Additional tax(2.34)";
$str .= "Additional tax(3)";

Can anyone help me up to extract the number from the string. I need an array format like this

Array ( [0] => 2.34 [1] => 3 );

Upvotes: 0

Views: 4801

Answers (1)

Sean Bright
Sean Bright

Reputation: 120644

This should do it:

<?php
    $str = '';
    $str .= "Additional tax(2.34)";
    $str .= "Additional tax(3)";

    if (preg_match_all('/\((\d+(?:\.\d+)?)\)/', $str, $matches) > 0) {
        var_dump($matches[1]);
    }
?>

Upvotes: 7

Related Questions