php - Manipulate massive array of strings -


from 1 of our vendors received dvd 12k+ images. before put them on our webserver, need resize, rename , copy them. i'm writing php cli program. , seems little stuck it...

all files fit pattern.

the copy , rename not problem, manipulation of strings is.

so symplify example code: lets suppose have array strings , want put them new array.

the original array looks this:

$names = array (  'fix1_vara_000.1111_fix2',  'fix1_varb_000.1111.2_fix2',  'fix1_varb_222.2582_fix2',  'fix1_varc_555.8794_fix2',  'fix1_vard_111.0x00(2-5)_fix2',  'fix1_vara_112.01xx(09-13)_fix2',  'fix1_varb_444.xxx1(203-207).2_fix2'     ); 

each string in array starts same fixed part in front , ends same fixed part in end fix1 & fix2 respectively. after fix1 there underscore followed variable part, followed underscore. i'm not interested in fixed parts or variable parts. cut away.

the remaining string can of following 2 types: if contains numbers , points: valid string , put in $clean array. eg: 000.1111 or 000.111.2 if string not have numbers , points in it, has several x's in , open closed bracked numbers , -. 444.xxx1(203-207).2

the numbers between brackets form series, , each number in series needs replace x's. strings should put in $clean array are:

444.2031.2

444.2041.2

444.2051.2

444.2061.2

444.2071.2

this part i'm struggling with.

$clean = array(); foreach ($names $name){     $item = trim(strstr(str_replace(array('fix1_', '_fix2'),'',$name), '_'),'_');     // $item values:     /*        * 000.1111,       * 000.1111.2,       * 222.2582,       * 555.8794,       * 111.0x00(2-5),       * 112.01xx(09-13),       * 444.xxx1(203-207).2       *        */      // if item has no x in it, can put in $clean array     if (strpos($item,'x') === false){         //this true first 4 array values in example         $clean[] = $item;     }     else {         //this last 3 array values in example         $b = strpos($item,'(');         $e = strpos($item,')');         $sequence = substr($item,$b,$e-$b+1);          $item = str_replace($sequence,'',$item);          /* part i'm stuck */         /* ------------------------------- */         /* should values in sequence variable , iterate on them:          *           * $names[5] ('fix1_vara_112.01xx(09-13)_fix2') want folowing values entered $clean array:          * value of $sequence = '(09-13)'          *           * 112.0109          * 112.0110          * 112.0111          * 112.0112          * 112.0113          *            */           } }  //now echo values in $clean: foreach ($clean $c){     echo $c . "\n"; } 

the final output should be:

000.1111 000.1111.2 222.2582 555.8794 111.0200 111.0300 111.0400 111.0500 112.0109 112.0110 112.0111 112.0112 112.0113 444.2031.2 444.2041.2 444.2051.2 444.2061.2 444.2071.2 

any "here i'm stuck" part appreciated.

like @stdob-- mentioned, regular expressions want. here's working version of code:

$names = array (  'fix1_vara_000.1111_fix2',  'fix1_varb_000.1111.2_fix2',  'fix1_varb_222.2582_fix2',  'fix1_varc_555.8794_fix2',  'fix1_vard_111.0x00(2-5)_fix2',  'fix1_vara_112.01xx(09-13)_fix2',  'fix1_varb_444.xxx1(203-207).2_fix2' );  $clean = array(); foreach ($names $name){     $item = trim(strstr(str_replace(array('fix1_', '_fix2'),'',$name), '_'),'_');     // $item values:     /*      * 000.1111,      * 000.1111.2,      * 222.2582,      * 555.8794,      * 111.0x00(2-5),      * 112.01xx(09-13),      * 444.xxx1(203-207).2      *      */      // if item has no x in it, can put in $clean array     if (strpos($item,'x') === false){         //this true first 4 array values in example         $clean[] = $item;     }     else {         // initialize empty matches array (i prefer [] array(), pick poison)         $matches = [];          // check out: https://www.regex101.com/r/qg4js4/1 see visually how works (also, regex101.com rad)         // uses capture groups, stored in $matches array.         preg_match('/\((\d*)-(\d*)\)/', $item, $matches);          // we've got array of values want have in our clean array         $range = range($matches[1], $matches[2]);          // since preg_match has our parenthesis , digits grabbed us, rid of string         $item = str_replace($matches[0],'',$item);           // regrettable variable names, idea!         foreach($range $number){             // here's gets ugly. you're wanting numbers work strings (have consistent length             // 09 , 13) work numbers (when create sequence of numbers). kind of             // thinking begets hackery. isn't fault, seems helpful point out.              // anyways, can use number of x's in string figure out how many characters ought             // adding. important because otherwise we'll end 112.019 instead of 112.0109.             // php casts '09' (int) 9 when run range() function, lose leading zero.             $xcount = substr_count($item, 'x');              if($xcount > strlen($number)){                 // function adds given number ($xcount, in our case) of character ('0')                 // end of string (unless it's given str_pad_left flag, in case adds                 // padding left side)                 $number = str_pad($number, $xcount, '0', str_pad_left);             }              // quick cheat padding empty string same number of x's counted earlier...             $xstring = str_pad('', $xcount, 'x');              // can add fixed string clean array.             $clean[] = str_replace($xstring, $number, $item);         }     } }  // happen prefer var_dump echo, again, mileage may vary. var_dump($clean); 

it outputs:

array (size=18)   0 => string '000.1111' (length=8)   1 => string '000.1111.2' (length=10)   2 => string '222.2582' (length=8)   3 => string '555.8794' (length=8)   4 => string '111.0200' (length=8)   5 => string '111.0300' (length=8)   6 => string '111.0400' (length=8)   7 => string '111.0500' (length=8)   8 => string '112.0109' (length=8)   9 => string '112.0110' (length=8)   10 => string '112.0111' (length=8)   11 => string '112.0112' (length=8)   12 => string '112.0113' (length=8)   13 => string '444.2031.2' (length=10)   14 => string '444.2041.2' (length=10)   15 => string '444.2051.2' (length=10)   16 => string '444.2061.2' (length=10)   17 => string '444.2071.2' (length=10) 

 

--edit-- removed warning strpos , ==, looks pointed out in comments.


Comments