Jump to content

Recommended Posts

Note: I can use a database, but I have about 10000 items I need to include in a search, and adding that into a database just isn't going to happen.....Is it possible VIA PHP to search a files contents? Say I'm making an item search system, and I want to search itemlist.txt, is it possible to make a PHP code to grab that file and then search it for the users input?

Link to comment
https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/
Share on other sites

Yes it is possible, but consider this -

 

Php is a parsed, tokenized, interpreted language. Database engines are complied code. It will be from 10 to 100 times slower to use php to search through a file than it will be for a database engine to search through a table. This becomes a bigger problem as the amount of data increases. What exact reason is there that you don't want to use a database?

$fileContent = file_get_contents('./itemlist.txt');
$pos = strpos($fileContent , $findme);
if ($pos === false) {
    echo "The string '$findme' was not found in the file.";
} else {
    echo "The string '$findme' was found in the file";
    echo " and exists at position $pos";
}

Yes it is possible, but consider this -

 

Php is a parsed, tokenized, interpreted language. Database engines are complied code. It will be from 10 to 100 times slower to use php to search through a file than it will be for a database engine to search through a table. This becomes a bigger problem as the amount of data increases. What exact reason is there that you don't want to use a database?

 

Well, if you know of any other ways for me to insert over 6,000+ game items into my database with an ID, Name, and INT then please do say so. So that's why I was hoping for a file instead, so I could just copy and paste. I want to show you an example: http://item-db.co.nr/ - That website is an item searcher, over 6,000 items just like how I want it. Game items of http://www.runescape.com to help you remember the codes, did they enter 6,000 items into a database manually?

 

$fileContent = file_get_contents('./itemlist.txt');
$pos = strpos($fileContent , $findme);
if ($pos === false) {
    echo "The string '$findme' was not found in the file.";
} else {
    echo "The string '$findme' was found in the file";
    echo " and exists at position $pos";
}

 

Thank you, but how do I make it display the data it found? Say you search for the item with the name: "Dragon Dagger" and it comes up with a result, and tells me the location/position of the file. I want it to also display the ID, so would that also be possible? And inside the file it would be listed something like this: "426 Dragon Dagger" - And when you search "Dragon Dagger" is should bring up the results: "ID ItemName" (Which would/should be listed in the file)

Just about every database management tool has a method of importing a text file of data with a specific format. You can even get mysql to do this with a query like the following (depending on the exact format of the data in the file) -

LOAD DATA LOCAL INFILE 'c:/your_file.csv' INTO TABLE your_table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n';

Loading the Data

To load the data into the table, you will first need to connect to theMySQL server, and select the correct database. Once you reach the connection, you will type the following query:

 

LOAD DATA LOCAL INFILE '/home/user/itemlist.txt' INTO TABLE items;

 

 

The exact path to the file must be specified after the word "INFILE" and be closed in single quotes. The example above shows a Linux/Unix path. The path for a Windows machine would be something like "C:\Documentsand Settings\user\itemlist.txt." Also, if the file was created by a Windows text editor, you should specify the \r\n line terminator in the LOAD DATA query with the following syntax:

 

LOAD DATA LOCAL INFILE '/home/user/itemlist.txt' INTO TABLE items LINES TERMINATED BY '\r\n';

 

Once the mysql query returns, you can check that your data was loaded correctly with the following query:

 

SELECT * FROM items;

 

This query will return with the complete record set for the "it" taemsble formatted into rows and columns.

 

 

LOAD DATA Options

The LOAD DATA statement has some options that will allow you to use analternate format for your text file and handle the importing of data.The example, above, uses the "LOCAL" option. This option says to look on the client machine for the file. If you are connecting to a remote MySQL server and omit the "LOCAL" option, the file must be located on the MySQL server and will be read directly from the server.

 

The "REPLACE" option says to replace the rows with the same primary key as the row in the file. A primary key is the value that uniquely identifies each record in a table. The "IGNORE" option says to skip anyrows that duplicate an existing row, based on the primary key. These two options are designated after the file name and before the word "INTO" such as:

 

LOAD DATA LOCAL INFILE '/home/user/itemlist.txt' REPLACE INTO TABLE items;

 

 

You can also specify that the fields will be separated by a character other than a tab, such as a comma separated value (CSV) file, with the "FIELDS TERMINATED BY" option. This option is specified after the tablename with the following syntax:

 

LOAD DATA LOCAL INFILE '/home/user/itemlist.txt' REPLACE INTO TABLE items FIELDS TERMINATED BY ',';

I'm sorry, but I have no clue what you mean. I keep trying it with this code: "LOAD DATA LOCAL INFILE '/home/user/names.txt' INTO TABLE pkitems;" - I select the table I want, and then click insert. Why isn't it inserting the item names? My file structure:

 

Dragon claws
Dragonplatebody
Wornout dragon gauntlets
Ruined dragon armour lump
Ruined dragon armour shard
Ruined dragon armour slice
Statius full helm
Statius platebody
Statius platelegs
Statius warhammer
Vesta's chainbody
Vesta's longsword
Vesta's plateskirt
Vesta's spear
Zuriel's hood
Zuriel's robe top
Zuriel's robe bottom
Zuriel's staff
Morrigan's coif
Morrigan's javelin
Morrigan's javelin(p)
Morrigan's javelin(p+)
Morrigan's javelin(p++)
Morrigan's leather body
Morrigan's leather chaps
Morrigan's throwing axe
Elite black full helm
Elite black platebody
Elite black platelegs
Arcane spirit shield
Blessed spirit shield
Divine spirit shield
Elysian spirit shield
Spectral spirit shield
Spirit shield
Corrupt dragon battleaxe
Corrupt dragon chainbody
Corrupt dragon dagger
Corrupt dragon longsword
Corrupt dragon mace
Corrupt dragon med helm
Corrupt dragon platelegs
Corrupt dragon plateskirt
Corrupt dragon scimitar
Corrupt dragon spear
Corrupt dragon sq shield
Santa's Top
Santa's Bottoms
Santa's Amulet
Robe top
Robe bottom
Hat
Regen. Bracelet
Skills necklace
Forinthry bracelet

 

My code:

 

LOAD DATA LOCAL INFILE '/home/user/names.txt' INTO TABLE pkitems;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.