Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading