views:

25

answers:

2

Hi there,

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)

I'm trying to create a php script which searches a text file for specific text. A person types in the specific text within a HTML form and the php script should search for that specific text within the text file.

The value of the input field of the HTML form is "username" and within the php document, the variable "$username" is the entered data, example shown below:

$username = $_POST['username'];

The text file below is called "rank_order.txt":

7 AULLAH1

13 SupremeGamer

13 AULLAH1

15 abc123

If a person types into the HTML form "AULLAH1", I'd like the php script to grab the first "AULLAH1" entry in the text file (e.g. It should grab the first line and not the thrid, as the first line is the first entry to contain the text "AULLAH1"). Also with that, when a person types in a specific text, it shouldn't simply grab the document or the text, it should grab the whole line: "7 AULLAH1" and place it into a php variable, maybe something like "$grabbed"? If possible at all.

Also with that, if it grabs the data from line 3, could a php variable represent line 3? If the data is grabbed from line 23, again could a php variable represent line 23 (and if it's grabbed from line 1, could a php variable hold the number 1? etc...)

All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)

Thank you.

+2  A: 

fopen, fgets, fclose will lead you in the direction and an intro into file handling in general.

http://php.net/fgets

Try explode on the space to get the columns. Once you're comfortable with that you can consider storing the data in a database.

Rob Olmos
Hi Rob Olmos, thanks for your response, I extremely appreciate it. ;) I'll see if I can work with fopen, fgets and fclose, that does seem pretty efficient. Thanks for your response, again I appreciate it. ;)
AUllah1
+2  A: 

Several solutions:

  1. Iterate through the document's lines, there are many how-to articles about this, this will enable you to count the lines. For each line check using regex for the string entered by the user

  2. Search the text of the document using regex, preg_match() will return the character position for what ever it is you are searching for. Once you know the character positions of the text your searching for you can count the number of occurrences of "\n" (new line character) before that search results position.

  3. Load the text into a database, then use the database's search functionality, like Full Text Search or a LIKE %...% statement to search for the text. Then some how count the number of new line characters (\n) up to the search text found.

jakenoble
Hi jakenoble, thanks for your response, I am extremely grateful of it. ;) At the moment, I don't have access to a database, as the host I'm using doesn't have the ability. After it searches for the text as well as the line, I simply want to echo the results, so I'm unsure if I'll need a database, none the less you are the expert! :) Again, thank you very much for the detailed response. ;)
AUllah1
Database would be good if you are reading the file a lot, and doing lots of searches. Opening the file and searching it using Regex is relatively slow compared to using a database.
jakenoble