How to split string which not having spaces or etc.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    How to split string which not having spaces or etc.

    I want to split a string which not having spaces or etc. Ex:START44.98. It always having "START" and values is different.ex:00 .00, 000.00,0.00. I want to take only the numeric value. How can i do that. I can't use split. Because value will vary.
    This is what i have done so far.

    [PHP]
    $mn=split(" ",$message) ;//start33.33 06-08-2008 15:02 1Stop
    $mnn = $mn[0];//start33.33
    [/PHP]
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    If you have the string "START123.4 56" and you want 123.456 out as a numeric value the following code will do just that.

    [PHP]
    $lcVar = "START123.4 56";
    $lcNum = str_replace("ST ART", "", $lcVar) ;
    $lnNum = floatVal($lcNum ) ;
    echo $lnNum;
    [/PHP]

    Alternatively you could use
    [PHP]
    $lcVar = "START123.4 56";
    $lcNum = substr($lcVar, 5) ;
    $lnNum = floatVal($lcNum ) ;
    echo $lnNum;
    [/PHP]

    There are many more solutions, some more complicated and some more thorough but if the format is guaranteed then the above offerings should work for you.

    Cheers
    nathj

    Comment

    Working...