Jump to content

Archived

This topic is now archived and is closed to further replies.

kkroo

preg_match for arrrays

Recommended Posts

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

this is what i currently have:

[code]
// 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);
}    
[/code]


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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.