Hi Forum, In my iPhone project,I have number of HTML files having different textual and image contents within each file. I need to implement search facility which would return results (HTML file names) having matched keyword. Can anybody have any clue, how can I achieve this? Thanks
Hi,
You can use this to search for text across multiple html files:
NSError *err = nil;
NSString *pageHTML = [NSString stringWithContentsOfFile:htmlfilePath encoding:NSUTF8StringEncoding error:&err];
if(err)
{
pageHTML = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:&err];
}
if([searchTxtField.text length])
{
NSRange range1 = [pageHTML rangeOfString:searchTxtField.text options:NSCaseInsensitiveSearch];
if(range1.location != NSNotFound)
{
NSString *stringFound = [pageHTML substringWithRange:range1];
}
else
{
//Load the next html page.
}
}
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =[localFileManager enumeratorAtPath:docsDir];
NSString *file;
NSString *stringYourelookingFor=@"String you're lookign for";
NSArray *matchingFiles=[NSMutableArray array];
while (file = [dirEnum nextObject])
if ([[file pathExtension] isEqualToString: @"html"])
NSString *currentFile = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:&err];
if ([currentFile rangeOfString:stringYourelookingFor options:NSCaseInsensitiveSearch]!=NSNotFound)
[matchingFiles addObject:file]
}
}
[localFileManager release];
At the end matchingFiles will contain all files matching stringYourelookingFor
Both of the other answers will be very slow and very memory intensive, since they require you to read every file on disk into memory.
I would suggest creating your own index of the HTML files before the user ever wants to search through them, and then simply searching through the index. This is something for which Core Data would be excellent.
CLucene is an implementation of the Lucene search library in C++. http://sourceforge.net/projects/clucene/ I have seen it used before in a corporate setting. It uses an inverted index and returns results based on relevance. You need storage for an index that it will create.