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 !!!!";
     }

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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;
?>

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.