Jump to content

Find all occurrence of a string in .txt file


Drugsxxx

Recommended Posts

try something like this:

 

 $fp = fopen( $file, 'r' );

    while ( !feof ( $fp) )
    {
        $buffer = stream_get_line( $fp, 1024, $delimiter );
        if(strpos($buffer,"North")==false or strpos($buffer,"North-East")==false)
               echo "Found IT !!!!";
     }

thanks for your answer kind sir.

 

Your code is working but i was thinking abut somthing like that:

 

I have a .txt file and data in it looks like this:

 

12.10.2011 19.58,North

12.10.2011 20,00,North

12.10.2011 20.02,North

12.10.2011 2010,North-East

 

and i wish to chek the file for occurrences and writein variable them somthing like this:

 

$North = 3

$North-East = 1

 

Is it posible to do??

You want to save the count of each string in two separate variables I think? Because the word "north" is also part of "north-east" you have to play smart here unless you're using regexes.

We use strtolower for a case-insensitive search.

 

$contents =  strtolower(file_get_contents('text_file.txt'));

$north_east = substr_count($contents, ',north-east');
$north = substr_count($contents, ',north') - $north_east;

for sure it is, you just have to track of the occurrences on a counter.

something like this:

 

$fp = fopen( $file, 'r' );
$north=0;
$north_east=0;
    while ( !feof ( $fp) )
    {
        $buffer = stream_get_line( $fp, 1024, $delimiter );
        if(strpos($buffer,"North")!=false)
               $north++;
        if(strpos($buffer,"North-East")!=false)
               $north_east++;
     }
echo "North->".$north.";North-East->".$north_east

Thx for your answer silkfire.

 

I wont to save number of occurences of those string in file. I will try your code i think this is what i mean. One more question if a take another direction in this file then code will look like this right??

 

$contents =  strtolower(file_get_contents('text_file.txt'));

$north_west = substr_count($contents, strtolower(',north-west'));

$north_east = substr_count($contents, strtolower(',north-east'));
$north = substr_count($contents, strtolower(',north')) - $north_east - $north_west;

Correct! To save the variables to a file, use the opposite of file_get_contents:

 

file_put_contents('values.txt', "North: $north\r\n
                                 North-West: $north_west\r\n
                                 North-East: $north_east");

Here is my regexp method:

 

<?php
$content = file_get_contents('my_file.txt');
preg_match("/north-east|north/i", $content, $matches);
$north = 0;
$ne = 0;
foreach($matches as $match){
if(strtolower($match) == "north") $north++;
if(strtolower($match) == "north-east") $ne++;
}
echo <<<OPT
North: $north<br />
North-East: $ne
OPT;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.