Jump to content

Treating a string variable as a function


andytan91

Recommended Posts

Hello guys, basically i am doing a search function based on regular expressions. What i have done is to store the string i want to regex as a variable. Variable is named as $new_string -> preg_split("/\.\(.*\)\.\(.*\)/", $file);

 

So inside the while loop, i would like to call another variable to store $new_string. However, I found out that the preg_split function stored inside $new_string doesn't work. If i use preg_split function directly inside the while loop it will work - > refer to  //$parts = preg_split("/\.\(.*\)\.\(.*\)/", $file);

 

I have troubleshooted it for like 1 hour plus.hope someone can help me 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("/\.\(.*\)\.\(.*\)/", $file);
    $parts = $new_string;
    echo $parts;
    
        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


?>







<form action="readdirectory.php" method='post'>Search Files:<p>
Hostname: <input type="text" name="argument"<p>
IP Address: <input type="text" name="argument1"<p>
Date: <input type="text" name="argument2"<p>
<p><input type="submit" name="submitFinal" value="Update">  <p>
</form>
</p>

 

I tried 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

 

The below codes are done in a while loop

 

Result:ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

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

  echo $parts;

 

Result:No Output

$part = eval($new_string);

echo $part;

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.