Reply
Search for a string in a txt file
Old 10-26-2009, 04:09 AM Search for a string in a txt file
Experienced Talker

Posts: 44
Trades: 0
I want to search for a value in a txt file and if there is one single match then should return the number from that line

I have this in a file

blabla 21
blablabla 23
bla 29
blablabla 3
I want to search in this file for bla. If bla is found in this file should return the number 3. If I search for blablabla and is found should return 23 and so on.
dan101 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 10-26-2009, 12:50 PM Re: Search for a string in a txt file
wayfarer07's Avatar
NYE-KEE

Posts: 3,152
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Let's say that the variable $text is equal to the content you gave above.

PHP Code:
function searchFor($search$text) {
    
$text_arr explode('\n'$text);
    foreach(
$text_arr as $line) {
        
$split explode(' '$line);
        if(
$split[0] == $search) return $split[1];
    }
    return 
false;//default
}
$filename "/path/to/something.txt";
$handle fopen($filename"r");
$text fread($handlefilesize($filename));

$num searchFor('bla'$text);//$num == 29 
This actually only works if there is only a single space per line, separating what you're searching for from the line number. If there is going to be multiple spaces in the text part, it is a bit more complicated.
__________________
Wayfarer | jQuery Tooltip Plugin | Mapbox: the jQuery Map
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola

Last edited by wayfarer07; 10-26-2009 at 12:55 PM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-27-2009, 08:01 AM Re: Search for a string in a txt file
mtishetsky's Avatar
King Spam Talker

Posts: 1,166
Name: Mike
Location: Mataro, Spain
Trades: 0
Quote:
$text = fread($handle, filesize($filename));
Can be dangerous on large files. You better use line-by-line reading and comparison.
__________________
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-27-2009, 12:57 PM Re: Search for a string in a txt file
wayfarer07's Avatar
NYE-KEE

Posts: 3,152
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
If what goes into the file is something you have exact control over, and you are using it to archive information, you are far better off using a database to do so.
__________________
Wayfarer | jQuery Tooltip Plugin | Mapbox: the jQuery Map
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-27-2009, 01:16 PM Re: Search for a string in a txt file
spyderwebtech's Avatar
Experienced Talker

Posts: 46
Trades: 0
Data storage should always be considered on the front-end of a project... not the end.

A database system might be preferable if there is a lot of data which must be stored and queried often. Sometimes for small datasets a flat file or even a memory caching system (such as memcache or APC) is the best course of action. It really all depends.

For small datasets, I have saved a serialize array to a flat file (and memecache) in which my keys were the index that I was looking for. Yes there can be some overhead, but it has worked pretty good in certain cases.

To save file:

Code:
$a = array();
$a['blah'] = 23;
$a['blah 123'] = 1234;
$a['blahblahblah'] = 33;

$z = file_put_contents('myfile.txt', serialize($a));
To review value of 'blah'

Code:
$a = unserialize(file_get_contents('myfile.txt'));
echo $a['blah'];
I am more a fan of using this technique with memory based caching but it would work with flat files too.
spyderwebtech is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Search for a string in a txt file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.11480 seconds with 13 queries