Jump to content

preg_match for arrrays


kkroo

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.