| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| DesignersTalk > PHP - Finding the occurences of a string in a file |
|
LinkBack | Thread Tools | Search this Thread |
|
|
#1 (permalink) | |
|
Registered User
Join Date: Mar 2005
Location: Manchester
Posts: 62
|
PHP - Finding the occurences of a string in a file
Hi all, I have this script which I have been developing @ university, it is supposed to search a for a directory or a file within a directory - the front end form hasn't been designed yet. Anyway, i'm concentrating on searching for a string within a file @ the moment as I have a deadline for 2 weeks. Within the While loop in the Else portion of the IF statement is supposed to find all occurences of the string assigned to $search, which could be anything - ultimately, but for test purposes I have assigned 'Test' as the string. Up to yet I have the script working so that it checks all files in a directory for this string and returns the path of the file. When the script has printed the file path, I have it so that it prints the number of lines the search string was found on. But, I need to get the script to function so that it does exactly above, but, alongside printing the line numbers the search term was found on (starting @ 1 not 0), it has to print the whole line of text with the search word in bold and uppercase. Please could anyone provide me with some pointers as i'm tearing my hear out over it. I kind of understand how to go about it, but I cannot turn this logic into code. Below is the code I have so far: Quote:
|
|
|
|
|
|
|
#2 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
I've had a bit of spare time, so here's a nice class that encapsulates what you want to do (feel free to reuse it wherever you like). It looks lengthier than it is because of the comments and doc tags but these should help you understand the class. I've broken down the functionality so you can provide a file to search (and it will just search for the search string in that file), or a directory, in which case it will recursively scan through the directory and sub-directories and look for the search term in each file it finds. PHP Code:
Here's some example usage: PHP Code:
Just one thing to remember...to search through a directory, make sure the directory path has a trailing slash (or the class will think its a file). Hope that helps. Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
Last edited by Luke Redpath : 31-03-2005 at 01:37. |
|
|
|
#3 (permalink) |
|
Registered User
Join Date: Mar 2005
Location: Manchester
Posts: 62
|
Non function apparoahc
Hi, thanks for your reply - brilliant. Only one problem, how do you approach this task in a non-functional way, i.e just so it can be implemented into my code above. All I am really stuck on is getting the else portion of the script to scan all files in a directory for a string, report the line number followed by a new line with the sentence the search term appears on in bold and uppercase. I can do all thr formatting (bold, uppercase), it is just the process of looping through the files and reporting the line number with the line containing the search string. Thanks again. |
|
|
|
#4 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
Hi, I created the class because I found your code quite difficult to read and understand - was quicker for me to write the class - it should be quite easy to take the class and implement it in place of your code (stick the class in a separate file and include it) - will make your code much easier, and hey its all part of the learning process, right? In answer to your specific question, take a look at the following functions from the class. For looping through lines of a given file (looping through files in a given directory and looping through directories recursively is handled elsewhere): PHP Code:
Note that I create a variable $lineNumber which increments as I loop through each line in the file - I can use this variable to log the line number when I call the addResult() function (which saves a match to an array). That's the first part dealt with - saving the line number along with each search result (note that I also save the contents of the line too, as well as the file name/path and the number of times the search term was found on that line (in case it is repeated more than once in a given line). In my presentation code (the "example usage" above), when I loop through the search results using foreach: Code:
, the resulting $result variable is a multidimentional array representing one result, with the following data available: filePath, lineContents, lineNumber, searchCount. Use $result['lineNumber'] to print out the line number. Use $result['lineContents'] to simply print out the contents of the line. And now the bit relating to highlighting the search term...instead of just displaying $result['lineContents'], pass it into the class function highlightSearchTerm(). This will highlight the search term each time it appears on the line. The simple bit of code which does that is this: PHP Code:
Hope that explains things better. Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
|
|
#5 (permalink) | |
|
Registered User
Join Date: Mar 2005
Location: Manchester
Posts: 62
|
Does amybody know what i'm doing wrong here: Quote:
I keep getting an error with the while loop and i'm not sure if I have coded the IF statement correctly. |
|
|
|
|
#6 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
A couple of pointers... First of all it would be a lot more helpful if you posted your code between PHP blocks instead of QUOTE blocks. Second of all, it helps if you post the error message you are getting. Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
|
|
#7 (permalink) | ||
|
Registered User
Join Date: Mar 2005
Location: Manchester
Posts: 62
|
$SearchString is the test string I want to find every occurence of and $file is from a previous section of code which identifies all files within a directory. This is then hyperlinked in the code below. PHP Code:
The errors I keep getting are the following: Quote:
and Quote:
I do not fully understand what this while loop is doing so I cannot help you with where the variables have come from. If anyone could help, it would be much appreciated. |
||
|
|
|
#8 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
OK, is there a reason why you are doing this: PHP Code:
Can you not just do this? PHP Code:
There shouldn't be any need to reset the array either. You shouldn't surpress errors when you can prepare for them instead. Try checking the file $file exists using the file_exists() function. Put that in an if...else statement, so if the file exists, carry on, if it doesn't, print out an error. Usually if you are trying to read a file using file() and then you are getting errors about $fileArray not being an array, it sounds like it cannot find the file you are trying to open. Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
|
|
#9 (permalink) | |
|
Registered User
Join Date: Mar 2005
Location: Manchester
Posts: 62
|
Still get error
Hi, i'm still getting errors, one in particular is this one: Quote:
I've modified the script to how you said: PHP Code:
All I need it to do is search a directory for a string which exists within $searchString. The $file is a variable which is used back on in the recursive function with holds any file name. Once the search string is found the script needs to report back to the user the line number (starting @ 1) and then print out the whole line the string was found on. So, the script needs to search every file in the directory of which it is placed. |
|
|
|
|
#10 (permalink) |
|
Barney army!
Join Date: Mar 2003
Location: London
Posts: 696
|
PHP Code:
You need to do the file_exists check before opening it with file, so put $fileArray = file($file) inside your if statement. PHP Code:
Always best to try and be a bit more descriptive with your errors, so perhaps "Error: file not found" would be better. Try this instead of the while loop: PHP Code:
Have you not considered using the class I posted in my first reply? Its tested, documented and works. Might save you a lot of time, and the implementation is a piece of cake. Luke Redpath .::. Software Engineer .::. Reevoo - Real Reviews From Real Customers
|
|
|
|
#11 (permalink) |
|
Registered User
Join Date: Mar 2008
Posts: 1
|
Hi, I realise that this thread is very very old. But I was hoping someone could help me a bit with a problem. The class Luke Redpath made works really really nice, but I want to expand a bit on it, and I'm just not sure how to do it. What I want is to print more then just the line where I find the search term I'm looking for. For instance, If I have a document like this: KUNDENR........: XXXX NAME: whatever ADRESS: hello thar EMAIL: myes@hi.thar items bought: 993429349 Whatever. 995454542 Whatever2. Now, I have a file filled with similar posts like that all under eachother (it is a long list of invoices), and I want to search for a spesific item, and print the whole post if I can find the itemnumber. Curently if I search for 9788203186233, it will print: Code:
But I want, for instance the 10 lines above that as well as the line with the search term. (even more spesifically, I want to find the string: "KUNDENR........: XXXX" that precedes the search string, so I can find all the customers that has bought a spesific item) I hope that made enough sense, if not, please ask any questions you need. Last edited by Kellendil : 18-03-2008 at 08:34. |
|
![]() |