Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Export Data to CSV (Using PHP)

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Export Data to CSV (Using PHP)

    Hi,

    I want to export data from a webpage, into a csv file. The data will be seperated by a pipeline "|".

    Is there any way this can be done using php?

    Cheers,
    Nick.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes.
    I don't suppose you want to provide any more details?
    How is the data input? How is it transferred to the script (GET, POST?) Is there a chance of the data having a pipe symbol in it? Is this a UNIX-, Windows- or old-Mac-style text file?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Hi Twey,

    I want to save the content of this site:
    http://www.mobiles4everyone.com/_exporths.asp

    into a .txt/.csv file, without saving any of the html code

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    PHP Code:
    <?php
      $d 
    "";
      
    $f fopen("http://www.mobiles4everyone.com/_exporths.asp");
      while(!
    feof($f)) $f .= fread($f1);
      
    fclose($f);
      
    $s = array();
      
    preg_match('[^<body]*<body[^>]*([^<\/body>]*)<\/body>.*'$d$s);
      
    $s $s[1];
      
    $f fopen("m4e.csv");
      
    fwrite($f$s);
      
    fclose($f);
    ?>
    I don't have KRegExpEditor on this laptop, so I haven't checked that regex
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    thanks, got an error for line 3 and a ton of errors on line 4.. ;(

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah - did it mention opening URLs like files?
    The easiest way is to set allow_url_fopen in your config file. The harder way is to implement a basic HTTP client like so:
    PHP Code:
    function getPage() {
      
    $errno;
      
    $errstr;
      
    $data "";
      
    $fp fsockopen("mobiles4everyone.com"80$errno$errstr30);
      
    $query "GET /_exporths.asp HTTP/1.1\r\n"
        
    "Host: mobiles4everyone.com\r\n\r\n";
      
    fwrite($fp$query);
      while(!
    feof($fp)) $data .= fread($fp1);
      
    fclose($fp);
      
    $data substr($datastrpos($data"\r\n\r\n"));
      return 
    $data;

    Replace
    Code:
      $f = fopen("http://www.mobiles4everyone.com/_exporths.asp");
      while(!feof($f)) $f .= fread($f, 1);
      fclose($f);
    with a call to this function:
    Code:
    $d = getPage();
    If you still get errors, please post them
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Ok, this is what i've done so far:


    view.php
    PHP Code:
    <?php
    ob_start
    (); 
    include (
    "http://www.mobiles4everyone.com/_exporths.asp");
    $inc ob_get_contents(); 
    ob_end_clean(); 
    $inc str_replace("<html>""""$inc");
    $inc str_replace("<head>""""$inc");
    $inc str_replace("<title>Untitled Document</title>""""$inc");
    $inc str_replace("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">""""$inc");
    $inc str_replace("</head>""""$inc");
    $inc str_replace("<body bgcolor=\"#FFFFFF\" text=\"#000000\">""""$inc");
    $inc str_replace("            """"$inc");
    $inc str_replace(" | ""|""$inc");
    $inc str_replace("<br />""<br/>""$inc");
    $inc str_replace("</html>""""$inc");
    $inc str_replace("</head>""""$inc");
    echo (
    "$inc");?>

    savefeed.php
    PHP Code:
    <?php 
    // define locations 
    $file 'http://www.mysite.com/view.php'
    $destination 'datafeed.txt'// change this for relative path to your desired server location 

    // retrieve & save the zip file 
    $fp fopen($file,"r"); 
    $fp2 fopen("$destination""w"); 
    while (!
    feof($fp)) { 
        
    $buf fread($fp1024); 
        
    fwrite($fp2$buf); 

    fclose($fp); 
    fclose($fp2); 
    ?>
    And i have uploadtosql.php which uploads database.txt to the mysql database.

    There's only 1 problem, and that is that there are empty lines in the html of the page http://www.mobiles4everyone.com/_exporths.asp which is causing empty lines of data in the .txt file... How could i remove them in the view.php page?

    PHP Code:
    $inc str_replace("EMPTY_LINE""NO_LINE""$inc"); 

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Instead of all those str_replace() calls, try simply:
    Code:
      $s = array();
      preg_match('<body\b[^>]*>(.*?)</body>', $inc, $s);
      $inc = str_replace(" | ", "|", str_replace("            ", "", $inc)); 
      $inc = $s[1];
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    I Get the error:

    Warning: preg_match() [function.preg-match]: Unknown modifier ']' in /home/crazy4/public_html/view.php on line 12

    when i open datafeed.txt

    PHP Code:
    <?php
    $mins 
    $_GET['freemins'];
    $txts $_GET['freetxts'];
    $page $_GET['page'];
    $hs $_GET['handset'];
    $hsclean str_replace(" ""%20"$hs);
    ob_start(); 
    include (
    "http://www.mobiles4everyone.com/_exporths.asp");
    $inc ob_get_contents(); 
    ob_end_clean(); 
      
    $s = array();
      
    preg_match('<body\b[^>]*>(.*?)</body>'$inc$s);
      
    $inc str_replace(" | ""|"str_replace("            """$inc)); 
      
    $inc $s[1];
    echo (
    "$inc");?>

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Whoops, I think I forgot to escape a slash.
    Code:
      preg_match('<body\b[^>]*>(.*?)<\/body>', $inc, $s);
    /EDIT: Hm, no, that doesn't fix it. I'll play around (I'm not too good with regex )
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •