Jump to content

eval() function not working in while loop


andytan91

Recommended Posts

Hello guys i used the eval function but it does not echo anything back in the while loop ..the $new_string function contains "preg_split("/\.\(.*\)\.\(.*\)/", $file);", same as $parts..i need some guidance, thanks!

 

<?php


  //"$DOCUMENT_ROOT"."new/"; 
  $current_dir = 'C:\xampp\htdocs\Audit_Reports';   //Put in second part, the directory - without a leading slash but with a trailing slash!
  $dir = opendir($current_dir);        // Open the sucker
  

$newArgument = $_POST[argument];
$newArgument1 = $_POST[argument1];
  
$string ='preg_split("/\.\(.*\)\.\(.*\)/", $file);';
$new_string = preg_replace("#\/\\\.\\\\\(\.\*\\\\\)#","/\.\\($newArgument\)",$string);
$new_string = preg_replace("#\\\.\\\\\(\.\*\\\\\)#","\.\\($newArgument1\)", $new_string);


       
  echo ("<p><h1>List of Audit Reports:</h1></p><hr><br />");
  while ($file = readdir($dir))            // while loop
  {
  
  $parts = preg_split("/\.\(CLIENT\)\.\(.*\)/", $file);
  // $part = eval($new_string);

  
        if (is_array($parts) && count($parts) > 1) {    // does the dissected array have more than one part
        $extension = reset($parts);        // set to we can see last file extension
        if ($extension == "Audit_Report" OR $extension == "audit_report")    // is extension ext or EXT ?
             echo "<a href=\"$file\" target=\"_blank\"> $file </a><br />";    // If so, echo it out else do nothing cos it's not what we want
        }
    }
  echo "<hr><br />";
  closedir($dir);        // Close the directory after we are done


?>

 

 

 

The below codes are done in a while loop

 

Result:ArrayArrayArrayArrayArrayArrayArrayArrayArrayArra yArrayArray

$parts = preg_split("/\.\(A\)\.\(A\)/", $file);

  echo $parts;

 

Result:No Output

$part = eval($new_string);

echo $part;

Link to comment
https://forums.phpfreaks.com/topic/210686-eval-function-not-working-in-while-loop/
Share on other sites

well the first thing I see wrong is

 

$string ='preg_split("/\.\(.*\)\.\(.*\)/", $file);';

 

You are trying to use a variable inside single quotes.  Single quotes do not parse variables, so you are assigning the literal string $file not the value of that variable.  You either need to put it inside double quotes or else break out of your single quotes and concatenate

 

Archived

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

×
×
  • 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.