Hi all,
I would like to find a word stored in a text file.
Structure: I have one file named keyWords.txt that stores some key
words I'm interested in finding. In addition I also have a file named
textOrigin.txt in which I store the text to search in.
I would like my prog to check if a certain word appears in the text
and than to tell me what line it found it in (if it did...).
My problem is that the script can't find the words I'm looking for. I
took one word from the word list and put it into the text file to be
searched, for some reason this word is not found by the prog. I used
'enter' at the end of each line. The word being used is on line 3 in
the keyWords.txt file. I have some reason to belive that the reason
lie here:
if ($pos)
{
echo " line $i: $storeWord[$n]\n";
}
I also tried it with if (!$pos === FALSE) {...} but nothing there
either...
Anyone?
Thank you very much for any help!
Dekers
the keyWords.txt file:
-------------------------------
Recording Site
Recording Type
INTRA
SUA
MUA
LFP
Acquisition Type
Windowed
Digitilized
Electrode Type
Tetrode
Metal
Pipette
Pipette Charakteristics
Tetrode Charakteristics
Electrode Tip Length (ìm)
Electrode Tip OD (ìm)
Bandwidth in Hz
Impegance in MegOhm
Number of Penetrations
Neurons Encountered
Neurones Analysed
Spike Amplitude in ìVolts
Spike Width in msec
Number of Pyramidal
Number of Interneurons
Background Activity
Max Modulation (Spikes/Sec)
Min Modulation (Spikes/Sec)
The textOrigin.txt file:
-------------------------------
I found the INTRA inside
My code:
*************** ****
PHP:--------------------------------------------------------------------------------
<?php
$filesource = "keyWords.t xt";
$fp = fopen ($filesource, "r");
$storeWord = array();
if ($fp)
{ $i = 0;
while (!feof($fp))
{
/*************** *************** ****
get the list of words to find and
store them in an array for later use
*************** *************** ****/
$line = fgets ($fp, 100);
$storeWord[$i] = $line;
$i = $i+1;
}
fclose($fp);
}
else
echo "File could not be found";
/*************** *************** ****
open the text source file, pick each line
and compare it to the complete list of key words
*************** *************** ****/
$filesource = "textOrigin.txt ";
$fp = fopen ($filesource, "r");
if ($fp)
{ $i = 1; //this is the line number
while (!feof($fp))
{
$textLine = fgets ($fp, 300);
/*************** *************** ****
compare all the words stored in the array
with each line in the origin file
*************** *************** ****/
for($n=0; $n<=count($stor eWord)-1; $n=$n+1)
{
$pos = strpos($textLin e, $storeWord[$n]);
if ($pos)
{
echo " line $i: $storeWord[$n]\n";
}
$i = $i+1;
}
}
fclose($fp);
}
else
echo "File could not be found";
?>
I would like to find a word stored in a text file.
Structure: I have one file named keyWords.txt that stores some key
words I'm interested in finding. In addition I also have a file named
textOrigin.txt in which I store the text to search in.
I would like my prog to check if a certain word appears in the text
and than to tell me what line it found it in (if it did...).
My problem is that the script can't find the words I'm looking for. I
took one word from the word list and put it into the text file to be
searched, for some reason this word is not found by the prog. I used
'enter' at the end of each line. The word being used is on line 3 in
the keyWords.txt file. I have some reason to belive that the reason
lie here:
if ($pos)
{
echo " line $i: $storeWord[$n]\n";
}
I also tried it with if (!$pos === FALSE) {...} but nothing there
either...
Anyone?
Thank you very much for any help!
Dekers
the keyWords.txt file:
-------------------------------
Recording Site
Recording Type
INTRA
SUA
MUA
LFP
Acquisition Type
Windowed
Digitilized
Electrode Type
Tetrode
Metal
Pipette
Pipette Charakteristics
Tetrode Charakteristics
Electrode Tip Length (ìm)
Electrode Tip OD (ìm)
Bandwidth in Hz
Impegance in MegOhm
Number of Penetrations
Neurons Encountered
Neurones Analysed
Spike Amplitude in ìVolts
Spike Width in msec
Number of Pyramidal
Number of Interneurons
Background Activity
Max Modulation (Spikes/Sec)
Min Modulation (Spikes/Sec)
The textOrigin.txt file:
-------------------------------
I found the INTRA inside
My code:
*************** ****
PHP:--------------------------------------------------------------------------------
<?php
$filesource = "keyWords.t xt";
$fp = fopen ($filesource, "r");
$storeWord = array();
if ($fp)
{ $i = 0;
while (!feof($fp))
{
/*************** *************** ****
get the list of words to find and
store them in an array for later use
*************** *************** ****/
$line = fgets ($fp, 100);
$storeWord[$i] = $line;
$i = $i+1;
}
fclose($fp);
}
else
echo "File could not be found";
/*************** *************** ****
open the text source file, pick each line
and compare it to the complete list of key words
*************** *************** ****/
$filesource = "textOrigin.txt ";
$fp = fopen ($filesource, "r");
if ($fp)
{ $i = 1; //this is the line number
while (!feof($fp))
{
$textLine = fgets ($fp, 300);
/*************** *************** ****
compare all the words stored in the array
with each line in the origin file
*************** *************** ****/
for($n=0; $n<=count($stor eWord)-1; $n=$n+1)
{
$pos = strpos($textLin e, $storeWord[$n]);
if ($pos)
{
echo " line $i: $storeWord[$n]\n";
}
$i = $i+1;
}
}
fclose($fp);
}
else
echo "File could not be found";
?>
Comment