Jump to content

preg_match won't accept a variable as a parameter!!!


saer

Recommended Posts

Heya I'm having some problem with my code, take a look:

 

static function ripDictionary($dictPath, $regexPattern, $numKeyWordsToAquire){

        $file = fopen($dictPath, "r") or exit("Failed to open the dictionary");

        
        echo $regexPattern ."<br>";
        echo $numKeyWordsToAquire ."<br>";
        

        while(!feof($file)){
            
            $line = fgets($file);

            if(preg_match($regexPattern, $line, $matches)){
                for($i=1; $i<=$numKeyWordsToAquire; $i++){
                    echo $matches[$i] . " ";
                }
                echo "<br/>";
            }
            else{
                echo "No match<br>";
            }

        }
        
        fclose($file);
    }

 

Ok, so when I run this nothing is returned, I just scroll down to see a tonne of blankness.

I think the problem has got something to do with me passing preg_match the $regexPattern variable,

because if I add in the line:

$regexPattern = "/(.*) (.*) \\[(.*)\\] (.*)/";   

before the while loop it works just fine.

 

Can anybody help me out, this is really annoying me. Could it be to do with the fact that the $regexPattern variable is being passed to this static function from a completely different class and .php file or that the regexPattern is coming from post data?

 

This is the HTML file I am using to submit the form too, just incase there are any abnormalities there take a look:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <form action="uploadDict.php" method="post" enctype="multipart/form-data">
          <label for="file">Filename: </label>
          <br />
          <input type="file" name="file" id="file" />
          <br />
          <label for="regexPattern">Regex Pattern: </label>
          <input type="text" name="regexPattern" id="regexPattern">
          <br/>
          <label for="numKeyword">Number of keywords to aquire: </label>
          <input type="text" name="numKeyword" id="numKeyword">
          <br/>
          <input type="submit" name="submit" value="submit" />
      </form>
  </body>
</html>

 

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.