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.
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.
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!
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
I don't have KRegExpEditor on this laptop, so I haven't checked that regexPHP Code:<?php
$d = "";
$f = fopen("http://www.mobiles4everyone.com/_exporths.asp");
while(!feof($f)) $f .= fread($f, 1);
fclose($f);
$s = array();
preg_match('[^<body]*<body[^>]*([^<\/body>]*)<\/body>.*', $d, $s);
$s = $s[1];
$f = fopen("m4e.csv");
fwrite($f, $s);
fclose($f);
?>![]()
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!
thanks, got an error for line 3 and a ton of errors on line 4.. ;(
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:
ReplacePHP Code:function getPage() {
$errno;
$errstr;
$data = "";
$fp = fsockopen("mobiles4everyone.com", 80, $errno, $errstr, 30);
$query = "GET /_exporths.asp HTTP/1.1\r\n"
. "Host: mobiles4everyone.com\r\n\r\n";
fwrite($fp, $query);
while(!feof($fp)) $data .= fread($fp, 1);
fclose($fp);
$data = substr($data, strpos($data, "\r\n\r\n"));
return $data;
}
with a call to this function:Code:$f = fopen("http://www.mobiles4everyone.com/_exporths.asp"); while(!feof($f)) $f .= fread($f, 1); fclose($f);If you still get errors, please post themCode:$d = getPage();![]()
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!
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
And i have uploadtosql.php which uploads database.txt to the mysql database.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($fp, 1024);
fwrite($fp2, $buf);
}
fclose($fp);
fclose($fp2);
?>
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");
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!
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");?>
Whoops, I think I forgot to escape a slash.
/EDIT: Hm, no, that doesn't fix it. I'll play around (I'm not too good with regexCode:preg_match('<body\b[^>]*>(.*?)<\/body>', $inc, $s);)
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