Forum Moderators: coopster

Message Too Old, No Replies

Files

Read and Extract

         

Ricky

8:06 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



OK, me again

I am try to read from file A
then read from file B

I need to read all the words from file B and extract them from A

for instance File A = the boy ran down the road
file B = ran, road

the ouput should of A should be boy down road

then output this to the screen, any takers
I have this so far, but i need to subtitute the variable $Key with a file.

<?

$key = array("/ the /", "/the /", "/ is /", "/ my /", "/ thy /", "/i /");

//load file into $fc array
$fc=file("psalm.txt");

//open output file and use "w" to clear file and write to it
$f=fopen("output.txt","w");

//loop through array using foreach
foreach($fc as $line)
{
$line = strtolower ($line); //change to lower case

//if (!strstr($line,$key)) //look for $key in each line
$line = preg_replace ($key , " " , $line);
fputs($f,$line); //place $line back in file
}
fclose($f);

if (!($fc=fopen("output.txt","r")))
exit("Unable to open file.");
while (!feof($fc))
{
$x=fgetc($fc);
echo $x;
}
fclose($fc);

?>

ergophobe

4:45 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are not reading in the input file before trying to write to the output file. I'm guessing you have no output. Is that right?

How big are these files? Can you just do this

$input = file_get_contents [php.net]($filename);
$output = str_replace [php.net]($keys, '', $input);

And then write the string to your output file?

Ricky

5:48 pm on Mar 6, 2005 (gmt 0)

10+ Year Member



No the output is fine, its just that I cant read in a file, and extract the words from another file.

File A is a list of words
File b is a Document

I need to extract all file A list fron file B

At the moment in the code below, I do it sucessfully with the variable $Key.

Any takers

ergophobe

8:13 pm on Mar 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Exactly.

$b = file_get_contents('fileb.txt');
$keys = explode(',',$b);
$a = file_get_contents('filea.txt');
$new_a = str_replace($keys, '', $a);

The problem with this code is it replaces strings, not just whole words. So if file b has

dog,foo

and file a has

Despite his dogged determination he was foobarred

The ouput is
Despite his ged determination he was foored

We can work around that with regular expressions or padding the search terms or whatever you need if you must search for whole words only.

Ricky

7:27 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



Yeh thanks, am still trying to get it done so anymore takers

ergophobe

7:02 am on Mar 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So are you saying that the partial word replacement wont' work for you?

mincklerstraat

12:09 pm on Mar 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I had more time I'd do like ergophobe does and teach you to fish, which is always cooler than having a fish; looks like you don't have much time either, so here's a fish (stick around here more and ergophobe will teach you to fish, it's worth it):

<?php
// keys.txt is in format 'word, anotherword, yetanotherword';
$keysfile = 'keys.txt';
$readfile = 'psalm.txt';
$writefile = 'psalmreplaced.txt';
$keys = file_get_contents($keysfile);
$words = explode(',' $keys);
$destination = file_get_contents($readfile);
foreach($keys as $v){
$destination = preg_replace('#[\W]'.trim($v).'[\W]#', '', $destination);
}
$handle = fopen($writefile, 'w');
fwrite($handle, $destination, strlen($destination);
fclose($handle);