Search code examples
phpstringsplittagspreg-split

Split string with PHP piece by piece


I have a string that I want to split like this:

Sinn.und.Sinnlichkeit.1995.German.DL.1080p.BluRay.x264-RSG <--- Original string
Sinn.und.Sinnlichkeit.1995.German.DL.1080p.BluRay.x264 <--- no match
Sinn.und.Sinnlichkeit.1995.German.DL.1080p.BluRay <--- no match
Sinn.und.Sinnlichkeit.1995.German.DL.1080p <--- no match
Sinn.und.Sinnlichkeit.1995.German.DL <--- no match
Sinn.und.Sinnlichkeit.1995.German <--- no match
Sinn.und.Sinnlichkeit.1995  <--- no match
Sinn.und.Sinnlichkeit <--- match
Sinn.und <--- no match
Sinn <--- no match

Until it has a API match.

Can you help me with that? I tried with preg_split, but it's not really working...

$string = 'Sinn.und.Sinnlichkeit.1995.German.DL.1080p.BluRay.x264-RSG';

$split = preg_split('/[.-]/', $string);

foreach( $split AS $explode )
{
   echo $explode;
}

Solution

  • You're splitting correct. All it takes is a for loop, building the required $arr from the parts as you go along.

    $string = 'Sinn.und.Sinnlichkeit.1995.German.DL.1080p.BluRay.x264-RSG';
    
    $split = preg_split('/[.-]/', $string);
    $arr = [];
    $brr = [];
    foreach( $split AS $explode)
    {
       $arr[] = $explode;
       // check $arr or implode($arr) with api
       // or push to
       $brr[] = $arr;
    }
    
    $result = array_reverse($brr);