Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 2
There are 1 users currently browsing forums.
PHP Scripting PHP is a scripting language for rapid web-development. It's a popular and powerful, OOP-capable language that is used by professionals. Discuss PHP here.

Reply
  #1  
Old 06-02-2009
New Born
 
Join Date: Jun 2009
Posts: 1
Rep Power: 0
jdrobinson is on a distinguished road
Some help with some search and replace

So, basically I have a text file with a ton of information and I'm trying to sort it so that I can put it into a database. I have been able to open it and put it into an array using the explode() function, but the problem is that the fields are comma separated as well as some of the content. For instance:

Name, Number, Keywords
Justin, 902-324-2344, "me, I, they"

So I more or less want to group the keywords together in one key of the array, but the commas are breaking that up. I do know however that the keywords are always surrounded by quotes, so I don't know if I can somehow change the code below to keep everything between quotes together, or if I need to do something else. So basically I'm wondering if I can get some assistance with a way of figuring this out.

PHP Code:
// File name
$filename "file.txt";

// Open file an write contents to the $contents variable
$fh fopen($filename"r") or die ("Unable to open file.");
$contents fread($fh5000);

// Close the opened file to conserve memory
fclose($fh);

// Place all of the data into a giant array.
$products_array explode(","$contents); 
Edit: So I've decided to switch the delimeter to a pipe character which alleviates me of this problem.
Reply With Quote
  #2  
Old 06-28-2009
Toddler
 
Join Date: Jun 2009
Posts: 5
Rep Power: 0
Brenden is on a distinguished road
Re: Some help with some search and replace

Instead of parsing it into PHP, if you're using MySQL you should look into the command LOAD DATA INFILE which can read a file like that directly and parse its results in the way you want them. Something like
Code:
LOAD DATA INFILE 'file.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
should do it for you. The file is relative to mysql's running directory. The mysql database table you're importing into needs to be the same fields as the data you are importing.

You can process the keyword array fields with PHP later on however you plan to do that.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
search and replace theoldnyx C and C++ Programming 10 09-19-2006 12:43 PM
find / replace wth encoded value chrisxkelley PHP Scripting 1 06-18-2006 04:47 PM
Google-ish search code bigalreturns Share a PHP Script 2 05-25-2006 05:24 AM
Make a search engine for your website with PHP zhisede PHP Scripting 4 10-25-2005 03:36 PM
Search YoungCoders FireFox Plugin! YoungCoder Chit Chat and Hangout 17 06-05-2005 04:58 PM