3raser Posted March 2, 2010 Share Posted March 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 2, 2010 Share Posted March 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1020659 Share on other sites More sharing options...
Dennis1986 Posted March 2, 2010 Share Posted March 2, 2010 $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"; } Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1020661 Share on other sites More sharing options...
3raser Posted March 3, 2010 Author Share Posted March 3, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1020727 Share on other sites More sharing options...
3raser Posted March 3, 2010 Author Share Posted March 3, 2010 Bump Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1020743 Share on other sites More sharing options...
PFMaBiSmAd Posted March 3, 2010 Share Posted March 3, 2010 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'; Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1020745 Share on other sites More sharing options...
teamatomic Posted March 3, 2010 Share Posted March 3, 2010 did they enter 6,000 items into a database manually? No, they wrote a script to do it for them. If you had >300 or so lines I'd say keep the text file but at 6K number you need a DB. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1020746 Share on other sites More sharing options...
Dennis1986 Posted March 3, 2010 Share Posted March 3, 2010 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 ','; Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1020783 Share on other sites More sharing options...
3raser Posted March 3, 2010 Author Share Posted March 3, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1021094 Share on other sites More sharing options...
3raser Posted March 4, 2010 Author Share Posted March 4, 2010 Here is my error video: http://www.youtube.com/watch?v=IShnOqX7M14 Quote Link to comment https://forums.phpfreaks.com/topic/193943-possible-to-search-a-file/#findComment-1021171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.