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>

 

Link to comment
Share on other sites

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;

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.