Re: insert text with no specific format into database table
| From: | Shawn McKenzie | Date: | Mon, 25 Feb 2008 17:15:49 +0000 |
| Subject: | Re: insert text with no specific format into database table | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-270076@lists.php.net to get a copy of this message | ||
Shawn McKenzie wrote:
> jeffry s wrote:
>> i have text file and a table created with
>>
>> create table word(
>> id int not null auto_increment primary key,
>> word varchar(50),
>> definition text
>> )
>>
>> the text contain list of words but not really in specific format
>>
>> word, some text definition
>> word, some text definition, some text definition, etc
>>
>>
>> i want to read the file line by line,
>> take the first word before comma (,) and insert into the word column in the
>> database
>> whatever after follow the first comma(,) will be inserted into the
>> definition column in the word table database.
>>
>> i am not sure how to read the file line by line in php.
>> and how to separate the line of text into two. divided by the 1st comma (,)
>> ..
>> my idea is using the explode(',' $text) function. but this one will separate
>> everything between a comma(,) into an array.
>> i wan't to know if there is another better way to do it..
>>
>>
>> any idea?
>> thank you!
>>
>
> Read about the file() function and also read about the explode()
> function's limit parameter.
>
> -Shawn
>
>
> if(($lines = file("file.txt"))) {
> foreach($lines as $line) {
> list($word, $definition) = explode($line, ',', 1);
> //insert word and definition SQL stuff here
> }
> }
Maybe I should read up on the limit parameter also :-0
list($word, $definition) = explode($line, ',', 2);