Jump to content


Photo

preg_match for arrrays


  • Please log in to reply
1 reply to this topic

#1 kkroo

kkroo
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 22 March 2006 - 08:06 PM

i am making a script that will get the var names of arrays through file(),

this is what i currently have:

// This function was found on http:php.net/preg_match
function array_preg_match($strRegEx = "", $arrHaystack = NULL, $boolNewArray = 0, $boolMatchesOnly = 0) {
   if (strlen($strRegEx) < 1) {
       return "ERR: \$strRegEx argument is missing.";
   }
  
   elseif ((!is_array($arrHaystack)) || (!count($arrHaystack) > 0)) {
       return "ERR: \$arrHaystack is empty, or not an array.";
   }
  
   else {
       unset($arrTmp);
      
       // search through $arrHaystack, and build new array
       foreach($arrHaystack as $key => $value) {
           if ($boolMatchesOnly) {
               if (preg_match_all($strRegEx, $value, $tmpRes)) {
                   $arrTmp[] = $tmpRes;
               }
           }
          
           else {
               if (preg_match($strRegEx, $value, $tmpRes)) {
                   if ($boolNewArray) { $arrTmp[] = $value; }
                   else { $arrTmp[$key] = $value; }
               }
           }
       }
      
       return $arrTmp;
   }
}




// My function
    function get_array_names ($file)
    {
        $file = file($file);
        

        $names = array_preg_match('(\$lang\[.*\])', $file);
        
        return array_values($names);
}    


I want it to return:

Array
(
[0] => $lang['Test']

[1] => $lang['Test1']

[2] => $lang['Test2']

)


BUT it returns

Array
(
[0] => $lang['Test'] = array(

[1] => $lang['Test1'] = array(

[2] => $lang['Test2'] = array(

)

can anyone help me out?

#2 kkroo

kkroo
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 23 March 2006 - 01:14 PM

bump!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users