Drugsxxx Posted November 11, 2011 Share Posted November 11, 2011 Is it pasible to find all occurrences of strings "North" and "North-East" in some txt file and save number of occurrences in some variables?? Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/ Share on other sites More sharing options...
cypher86 Posted November 11, 2011 Share Posted November 11, 2011 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 !!!!"; } Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287318 Share on other sites More sharing options...
Drugsxxx Posted November 11, 2011 Author Share Posted November 11, 2011 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?? Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287336 Share on other sites More sharing options...
silkfire Posted November 11, 2011 Share Posted November 11, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287342 Share on other sites More sharing options...
cypher86 Posted November 11, 2011 Share Posted November 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287344 Share on other sites More sharing options...
cypher86 Posted November 11, 2011 Share Posted November 11, 2011 you can also use regex for the occurrences without read line by line the file. Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287346 Share on other sites More sharing options...
Drugsxxx Posted November 11, 2011 Author Share Posted November 11, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287347 Share on other sites More sharing options...
silkfire Posted November 11, 2011 Share Posted November 11, 2011 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"); Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287353 Share on other sites More sharing options...
The Little Guy Posted November 11, 2011 Share Posted November 11, 2011 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250929-find-all-occurrence-of-a-string-in-txt-file/#findComment-1287354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.