I want to read the content from website and save then into a csv file in php, can anyone please tell me how I can do this.
How do you want to save a website's content as csv file? CSV means comma separated values, and it's really easy to save things as csv file but a website's content?
You say "the content from a website" - normally you'll start reading one sites content, which includes html markup, scripts and styles. Or do you only want to get the text contents or some meta data?
If your server supports opening urls via fopen I'd try this one (php.ini option: allow_url_fopen) - otherwise you'll have to use cURL or something.
Here's some more information about reading websites in php.
Regarding the storage of websites as csv I think you should be more precise what you want to achieve.
Regards, Daniel
There's no instant-magic answer for your question. We (You) need to know which website is in question, how the table is presented. If you know everything about your scenario, you should use PHP's DOM functions and parse your table then export it to a CSV.
Related
I wonder if I can create a php file with php code (my project is to write a php based online poker game) and it would be nice if I could add/delete tables from php, but in order to do that I need some code to generate the php file which will be associated with that specific table. (code on every table would be the same, just need something that will allow me to create a .php file for the life time of a table). Also can you tell me how to php-delete it after wards? Thanks ahead.
You can do this simply by having the code written to a file with
file_put_contents($filename, $content).
This will create a new file if it doesn't exist. If it exists it will truncate the file and write the new content.
To read dynamic code you could use eval($codeString). This would interpret the $codeStringas code. NOT RECCOMENDED because if there is ANY user input involved in the $codeString, it would be a huge security risk! (Read the WARNING below!)
To get the contents from a file, you could use $fileContents=file_get_contents($filename)
If you want to write to files with appending text and so on, you need to get deeper in to the php filesystem. A nice place to start is w3 schools: http://www.w3schools.com/php/php_ref_filesystem.asp
You should look at three major functions of writing to files:
fopen();
fwrite();
fclose();
Warning!
Reading dynamic generated code, whether it is from files or just strings, can be really dangerous. Especially if it gets any kind of user- or dynamic input. This is because php-files are capable of editing, creating and deleting ALOT on your server. I would recommend you to find an alternate solution!
You can use file_put_contents Or touch.
file_put_contents() will create the file if not exists and will write provided data in to the file.
touch() will create the file.
I'm creating a php based web application which allows the user to upload a PDF file. This file will then be read and checked for certain data (text).
The problem is I can't figure out how to even open a PDF file in PHP. There are some PDF libraries mainly for creating PDF's, but they don't seem to be very good at reading them.
An alternative solution would be to use an already available solution in Python or something else (as described in other threads on this site) but I'd really like to stay as much as possible in PHP as I intend to later export the data to mysql, etc.
Any input on how to read a PDF and extract data from it would be much appreciated.
I personally haven't tried this out, but it looks like this one works: http://www.pdfparser.org/documentation
It's just a matter of downloading and telling your code to include it, just like the documentation shows.
Or you could try the class.pdf2text.php found in http://www.phpclasses.org/browse/file/31030.html
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I export tables to excel from a webpage
EDIT: Just to clarify, it does not HAVE to be an Excel file, but it has to be a spreadsheet file which is able to be opened/edited in Excel.. So whatever would accomplish this most easily is the answer I seek :)
I need to convert an HTML table to some sort of downloadable spreadsheet (preferably for Office Excel). I can do this in jQuery, but would prefer to do it in plain JavaScript. I've searched around and have found a lot of info about doing the reverse (spreadsheet->html), but I need to create a downloadble spreadsheet file of a dynamically built table. Any points for how I could accomplish this would be much appreciated! :)
Don't do this in JavaScript. You need to do this in a server-side scripting language like PHP.
The easiest way to prepare data for importing into a spreadsheet is going to be creating a CSV file. Here's an example CSV file which will open fine in Excel:
column_a,column_b
1,2
5,7
8.988,abcdef
CSV files are just plain text, with the fields separated by commas, so they are easy to create. If the data in your fields needs to contain commas, double quote marks, newlines, or a few other special cases, then things get more tricky.
If you really want to create an Excel-format spreadsheet, this is pretty difficult and you're going to want the help of a library. Here's how I would find a PHP Excel library: http://google.com/search?q=php+create+excel+spreadsheet
You can't do this with JavaScript because it cannot create a downloadable file.
You must do this on the server. Any HTML page that is a table will automatically be converted BY EXCEL ITSELF if you send out the correct MIME header before streaming out the HTML file.
You can't do it all in Javascript. You can write a jQuery function to convert a table to something like a CSV format, but you won't be able to tell the browser to let the user download it. You'll have to POST it to a server which can then serve back the file.
See this question for details.
I can be mistaken, but it may be possible using data: URI scheme
<a href="data:text/csv;charset=utf8;base64,...encoded_data...">
download as Excel
</a>
It is not cross-browser, but can help.
For example, i am uploading a word file with some FORMATTED contents in the database. The content in the word document is aligned.
I done up to the above level . My issue is how can i able to view the CONTENTS AS IT LOOKS EXACTLY (means the exact formatted contents) IN A BROWSER.
Kindly help me out of this issue.
Thanks in Advance
Fero
You may stream the content in the body of the request as an attachment setting the correct MIME Type. If the user's client is configured to handle the content type it will show (after asking for permissions).
PHP MIME Content Type
Word is a format for word processing, whereas the browser is a client for displaying web pages. So no, you can't. There are some similarities between the two formats, so you can transform between them, but usually at a loss. Since Word is a proprietary format, transforming it to html can be tricky, but you can generally use open office for the job.
Another alternative is, instead of uploading the file, upload the content of the file through the use of a javascript WYSIWYG editor like TinyMCE. Since you will be storing the HTML markups that the editor converts from the formatted contents that you copy-paste in it, it will be very straight-forward to display the contents.
If the content doesn't need to be edited, why not convert it to a .PDF/.JPG on the fly, or do it once upon upload and cache the result?
I just want to know if it's possible. How do I save an html page and all of its contents to a database? like for example, I have a database wherein it consists all HTML pages.
Just want to know if its possible. And how to retrieve it too. We're using PHP as our language.
Thank you.
Well, you'll need to:
Grab that page by using a HTTP request, just like your browser does
Parse that HTML to find external resources (script, img, object, etc)
Grab those external resources
Save all them on your database into a BLOB field
Optionally alter your original HTML document, to change that resources location
Is this what you are trying to do? http://www.phpfreaks.com/forums/index.php?topic=219271.0
You can simply store the $out in the db instead of saving as html
Assuming MySQL, here is the way to connect to the database and write data into it.
PHP and MySQL
HTML is just text. You can tore it in a database in a TEXT field.
There are plenty of DBMS you can use and plenty of ways to do it.
You can have a look at the PDO extension to directly consume a MySQL or SQlite connection for instance.
You can also use an ORM like Doctrine
If you are trying to save the final results of your PHP script (ie. what is sent to the browser) you will need to look into Output Buffering.
As others already suggested, yes its possible to save HTML pages inside databases like mysql or sqlite etc. Another way you can perceive "databases" is flat files. Therefore, just like web crawlers or tools like wget/curl that crawls(and download) pages to disk, you can program something like that in PHP (using libraries such as curl) and save those pages to your disk. How to retrieve?? just display them with web browser OR do normal opening of file , display the contents and closing the file, all with PHP.