php - turning this trainwreck of a function into a recursive one -


i've been trying build recursive function better part of day now, can't seem work way want.

first, have property holds data function have access:

$this->data 

and have string intention turn relative path:

$path = 'path.to.%id%-%folder%.containing.%info%'; 

the part of string this: %value% load dynamic values found in $this->data property (like so: $this->data['id']; or $this->data['folder'];

and make things interesting, property can reference again so: $this->data['folder'] = 'foldername.%subfolder%'; , have 2 %values% separated - have left alone.

so problem, i've been trying make recursive function load dynamic values data property, , again if new value contains %value% , on until no more %value%'s loaded.

so far, i've been able come with:

    public function recursivefolder( $folder, $patharr = null )     {         $newpatharr = explode( '.', $folder );         if ( count ( $newpatharr ) !== 1 )         {             foreach( $newpatharr $id => $folder )             {                 $value = $this->recursivefolder( $folder, $newpatharr );                 $resultarr = explode( '.', $value );                 if ( count ( $resultarr ) !== 1 )                 {                     foreach ( $resultarr $nid => $result )                     {                         $nvalue = $this->recursivefolder( $result, $newpatharr );                         $resultarr[$nid] = $nvalue;                     }                  }                 $resultarr = implode( '.',$resultarr );                 $newpatharr[$id] = $resultarr;             }         }         else         {             $pattern = '/%(.*?)%/si';             preg_match_all( $pattern, $folder, $matches );             if ( empty( $matches[0] ) )             {                 return $folder;             }             foreach ( $matches[1] $mid => $match )             {                 if ( isset( $this->data[$match] ) && $this->data[$match] != '' )                 {                     $folder = str_replace( $matches[0][$mid], $this->data[$match], $folder );                     return $folder;                 }             }         }          return $newpatharr;     } 

unfortunately not recursive function @ grinds halt when has multiple layers of %values%, works 2 layers -barely-. (i coded work @ bare minimalistic level point).

here's how should work:

it should turn:

'files.%folder%.blog-%type%.and.%time%' 

into:

'files.foldername.blog-post.and.2013.feb-12th.09' 

based on this:

$data['folder'] = 'foldername'; $data['type']   = 'post'; $data['time']   = '%year%.%month%-%day%'; $data['year']   = 2013; $data['month']  = 'feb'; $data['day']    = '12th.%hour%'; $data['hour']   = '09'; 

hope can help! jay

i don't see need solved recursively:

<?php function putdata($str, $data) {     // repeat replacing process until no more matches found:     while (preg_match("/%(.*?)%/si", $str, $matches))     {         // use $matches make replaces     }     return $str; } ?> 

Comments