phpstringtext-extractiontext-parsingdelimited

Parse colon delimited string into variables based on a value's preceding label


We are trying to get certain parts of a string.

We have the string:

location:32:DaD+LoC:102AD:Ammount:294

We would like to put the information in different strings. For example $location=32 and $Dad+Loc=102AD

The values vary per string, but it will always have this construction: location:{number}:DaD+LoC:{code}:Ammount:{number}

How do we get those values?


Solution

  • Easy fast forward approach:

    $string = "location:32:DaD+LoC:102AD:Ammount:294";
    
    $arr = explode(":",$string);
    
    $location= $arr[1];
    $DaD_LoC= $arr[3];
    $Ammount= $arr[5];