Search code examples
phpstringtext-parsingdelimited

Get the second and third values in a delimited string and assign to variables


I have the following string:

CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550

which in the code below corresponds to $track_matches[0][0].

The only constant-length field is the first (CAE33D8E804334D5B490EA273F36830A9849ACDF), which is 40 characters long. I am trying to get the values xx and yy which are an unknown length and value along with the rest of the column.

So I am trying something like this:

$seperator = '|';
$end_seed = strpos($track_matches[0][0], $seperator, 41);
$seeders[$i] = substr($track_matches[0][0], 41, $end_seed - 41);
$end_leech = strpos($track_matches[0][0], $seperator, $end_seed + 1);
echo "end_seed" . $end_seed . " end_leach: " . $end_leech;
$leechers[$i] =  substr($track_matches[0][0], $end_seed +1, $end_leech - $end_seed - 1);

The problem is that the line $end_leech= doesn't seem to work properly (and doesn't recognize the $seperator) and retuns the entire line ($track_matches[0][0]) as its value when echo'd while $end_seed returns the proper value. Why is this happening? How do I fix it?


Solution

  • try :

    $myString="CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550";
    $splitString=explode('|',$myString);
    $xx=$splitString[1];
    $yy=$splitString[2];
    

    of course you can replicate manually with strpos, substr etc but will take more effort