How to read .csv file? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a CSV file. If I open it with a standard text editor such as notepad or excel, I can read the strings. But I can't read it with my PHP code (there is a symbols font error). How can I fix it?
My PHP code:
<?php
$file = fopen("data.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
My CSV file
Thank all! I found the answer http://www.practicalweb.co.uk/blog/2008/05/18/reading-a-unicode-excel-file-in-php/

The file encoding appears to be UCS-2 LE, from which UTF-16 developed1. (also see this answer).
Save the file with UTF-8 encoding. Instead of notepad, try using a different text editor like Notepad++. It has a menu for choosing the encoding of the file. Then the PHP code can read the characters as desired. See demo here.
1https://web.archive.org/web/20060114213239/http://www.unicode.org/faq/basic_q.html#23

Related

How to convert a pdf file to bytecode in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
Dear I want to convert a pdf file to bytecode to send the byte code through json but I am not able to convert the pdf to bytecode in php if there are any solutions please assist me..
Thank you.
You can try something like this:
file_get_contents will get you the whole file into a string.
$fileContents = file_get_contents($file);
str_split will convert it into an array of one-character strings.
$byteArray = str_split($fileContents);
You can check official documentation as well.

How do I convert Hindi Text to UTF-8? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've got an Excel file with text in Hindi which I'm trying to read via PHP but I only get gibberish out of it.
When I open the .xls File with Numbers, the text looks like this:
But when I copy and paste this exact text here it looks like this:
ß[+kq'kh ge ij fuHkZj djrh gSAÞ
When I read it via PHP I get exactly the same gibberish text.
Is this an encoding problem in the .xls file and is it somehow possible to extract a UTF-8 encoded version from it?
Jaswinder Singh's comment fixed the Issue!
The text is written in a font called "Kruti Dev 010" which is just normal text but displayed as hindi letters.
By using this tool I could recover the texts to unicode.
Thanks Jaswinder!

How to make specific columns of a csv file read only with php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wrote a script to export some columns(id and name) of a mysql table so that scores of students can be inserted. But I want to make the id and name columns read only. I searched but could not find a solution. Please, how can I do this?
You cannot do this.
A file is just a stream of bytes stored (in HDD/memory). You read these bytes and interpret it as you want. In case of csv file you read one line and the split this based on separator.
The concept of read only comes from OS. Where you mark a file as read only to ensure users without proper permission do not edit the file.
While writing a program you open a file in read only mode using r option this is just a way of telling you do not intend to write anything to file and fwrite should not be allowed (throw exception) on this stream.
What you are asking is to make certain bytes of the files to be read only while others should be editable. OS or file stream have no way differentiating between the two.

How to generate new page on my website using PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can i create new page on my website using PHP? I need PHP-code, which can create a new page. For example, i've got index.php and i need to create index2.php w/ PHP. Thanks.
Like above answer you can use fopen() and fwrite() to build a new .php file, or you could try using file_put_contents() which would also generate a new file if it doesn't exist.
file_put_contents($filename, $content);
Example
$filename = "index2.php";
$content = "<h1>hello world</h1>";
file_put_contents($filename, $content);
Use fopen() to create a file, and fwrite to edit it, more info here :
http://www.w3schools.com/php/php_file_create.asp
Open Notepad (if your do not have any other softwares like Notepad++ or Dreamweaver for editing PHP code), write your code there, Save the text file with .php extension in the same directory where your index.php file is located.

How to extract complete html source code and save it in a text file by using php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to extract the source code from a website and want to save it in a text file on wamp server. So kindly help me.
It is not very complicated, you can get the content of a file by calling file_get_contents() this includes files on the web.
Then you just need to write the content to a file and presto you have extracted the .html source and saved it in a text file by using php.
<?php
// Open a webpage
$homepage = file_get_contents('http://www.example.com/');
// echo the homepage to see the content.
echo $homepage;
// Set the filename
$file = 'hp.txt';
// Write the contents back to the file
file_put_contents($file, $homepage);
?>
Hope this is what you need.

Categories