Code:
function genWordFilter()
{

   $file = fopen("common.txt","r");

   while (!feof($file))
   {
      $commonwordsarray[] = fgets($file);
   }

   fclose($file);

   return $commonwordsarray;   
                         
}


Keep getting the following errors:
Quote:
Warning: feof(): supplied argument is not a valid stream resource in C:\Users\Ad
min\Desktop\Nemele\nemele.php on line 192

Warning: fgets(): supplied argument is not a valid stream resource in C:\Users\A
dmin\Desktop\Nemele\nemele.php on line 193


which would indicate that fopen failed but it shouldn't have failed given that the file exists AND is in the same directory.


And yeah, I know it's important to check for file existence but at the moment I'm just writing some dirty code to see if my ideas are even feasible or useful.


[edit]
REGEX QUESTION!

Say I have a file containing a bunch of lines of data in the form of:

[A WORD] #

where [A WORD] is literally a word in brackets and # is a number.

How would I go about regex replacing a given line with one where the number is incremented by 1? Or would regex not be the way to go?
TsukasaZX wrote:
Say I have a file containing a bunch of lines of data in the form of:

[A WORD] #

where [A WORD] is literally a word in brackets and # is a number.

How would I go about regex replacing a given line with one where the number is incremented by 1? Or would regex not be the way to go?

Regex isn't a very good way of doing that, since there's a well-defined format for the data. Regex is useful for pulling out data when you're not quite sure how if might be formatted. In this case, you can just split the text and parse, and write back. Python example since I don't know PHP at all:

Code:
l = []
with open("foo.txt","r") as f:
    for line in f:
        (word, number) = line.split()
        l.append( (word, int(number) + 1) )

..at which point you can do whatever with the contents of the list l.
There is nothing inherently wrong with your code. Assuming these two files:

nemele.php:

Code:
<?php

function genWordFilter()
 {

    $file = fopen("common.txt","r");

    while (!feof($file))
    {
       $commonwordsarray[] = fgets($file);
    }

    fclose($file);

    return $commonwordsarray;   

 }

var_dump(genWordFilter());

?>


common.txt:

Code:
foo
bar
baz
bat


Running the above on the command-line yields:

Code:
D:\Documents\Desktop\Nemele>php nemele.php
array(5) {
  [0]=>
  string(5) "foo
"
  [1]=>
  string(5) "bar
"
  [2]=>
  string(5) "baz
"
  [3]=>
  string(5) "bat
"
  [4]=>
  bool(false)
}


However, it does seem like overkill - the following does the same:


Code:
<?php

function genWordFilter()
 {

    return file('common.txt');   

 }

var_dump(genWordFilter());

?>


You can use regular expressions for your second example if you wanted. preg_replace_callback performs a replacement using a user-supplied callback.


Code:
<?php

# Get the contents of the file "numbers.txt".
$numbers_file = file_get_contents('numbers.txt');

# Display:
echo "Before: \r\n{$numbers_file}\r\n\r\n";

# This is a callback function that will be used to increment the values in the number file.
function increment_values($matches) {
   return $matches[1] . ($matches[2] + 1);
}

# Replace the values using the above callback.
$numbers_file = preg_replace_callback('/^(\[.*?\]\s*)([0-9]+)/', 'increment_values', $numbers_file);

# Display:
echo "After: \r\n{$numbers_file}\r\n"

?>


I do agree that regular expressions are not always the best tool for the job, but in this instance they do pretty well.
Thanks for the opinions, Tari and Benryves. Tsukasa, thoughts?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement