Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2001, 03:02 PM
Mincer's Avatar
Mincer Mincer is offline
 
Join Date: Nov 2001
Location: London
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

1) Is there an easy way to strip "'s from a text file using php?

It's not really feasible to do a s/r using a text editor as the files will be uploaded by a user and actioned directly rather than them passing through my control first.


2) How can I read a line at a time from a csv file before exploding it?

Say grab all characters into a string until \n and then process, then repeat in a loop until EOF

(I could do this in c++ - albeit a bit rusty now - but not really sure in php)

Any solutions greatly appreciated.

Matt.

--------------------------

If there's no way of easily reading the file a line at a time, is it a really bad idea to read a 1000+ line file in to an array in one go, and then exploding it into lines on \n, and then exploding each line at a time on the delimiter?

Lastly, if I do this, is this a good way of exploding a line at a time until EOF??

PHP Code:

$filename 
"file.txt";
$fp fopen($filename"r");
$file_contents fread($fpfilesize($filename));
fclose($fp);

$lines explode("\n"$file_contents); 

// loop for as long as $lines is not EOF

while($i <= sizeof($lines)) {        

    
// explode to get each field from line
    
$data explode(";"$lines[$i]);

    
// do some stuff to enter fields into DB

    // increment to continue looping
    
$i++;
    

I know that this will work ok for small files, but what would be a cut-off for 'small file' ??
Reply With Quote
  #2  
Old 12-17-2001, 03:17 PM
Mark Hensler's Avatar
Mark Hensler Mark Hensler is offline
 
Join Date: Oct 2001
Location: California
Posts: 205
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

1) try this:
PHP Code:
$text str_replace("'s"''$text); 
2) try this:
PHP Code:
$fd fopen ("file.txt""r");
// loop until EOF
while (!feof ($fd)) {
    
// read one line, up to 4KB
    
$buffer fgets($fd4096);
    
    
// explode to get each field from line
    
$data explode(";"$buffer);
    
    
// do some stuff to enter fields into DB
    
}
fclose ($fd); 
PHP DOCS: fgets(), fopen(), feof(), str_replace
Reply With Quote
  #3  
Old 12-17-2001, 03:37 PM
Mincer's Avatar
Mincer Mincer is offline
 
Join Date: Nov 2001
Location: London
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the quick response.

The first question, I needed to replace the quotation char " with nothing. I guess the principal is the same though. Can I just use \" ??

PHP Code:
$text str_replace("\""''$text); 
EDIT: Yes it is.

-----------------

Secondly, just so I understand correctly... (always a bonus in my case )... fgets() reads one line up to endline?? (nix or win or both??)

Thanks.
Reply With Quote
  #4  
Old 12-17-2001, 03:59 PM
Mincer's Avatar
Mincer Mincer is offline
 
Join Date: Nov 2001
Location: London
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

In case anyone cares....

I added:

PHP Code:
$fd fopen ("data.csv""r");

// loop until EOF
while (!feof ($fd)) {

    
// read one line, up to 4KB
    
$buffer fgets($fd4096);
    
    if (
$buffer == '') exit();  // 2 single quotes
    
else {
    
        
// explode to get each field from line
        
$data explode(";"$buffer);

        
$data str_replace("\""''$data);
    
        
// do some stuff to enter fields into DB
    
    
}
    
}

fclose ($fd); 
To stop it outputting garbage if there are empty lines at the end of the file. (in my case empty lines will not occur in the file up till then).
Reply With Quote
  #5  
Old 12-18-2001, 03:20 AM
Mark Hensler's Avatar
Mark Hensler Mark Hensler is offline
 
Join Date: Oct 2001
Location: California
Posts: 205
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

fgets() will read up to the number of bytes you specify, until it reads a EOL, or until EOF (which ever comes first). PHP is wise to the win/unix differences, and will watch for \n on unix and \r\n on win for EOL.
Reply With Quote
Reply


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

Forum Jump


All times are GMT. The time now is 04:06 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03560 seconds
  • Memory Usage 2,225KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete