Read Persian (Unicode chars) text file using php - php

I am reading one Persian text file (using PHP) with the help of below code:
/* Reading the file name and the book (UTF-8) */
if(file_exists($SourceDirectoryFile))
{
$NameBook = "name.txt";
$AboutBook = "about.txt";
$myFile = "Computer-Technolgy/2 ($i)/".$NameBook;
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo 'Name file: '. $theData.'<hr/>';
}
name.txt file contents :
آموزش شبكه هاي کامپيوتري (LEARNING NETWORK)
Name file: ����� ���� ��� ��������� (LEARNING NETWORK)

The reason you are seeing this is because you are just echoing the contents raw. Your browser will need more information, in order to display the message in its correct form.
The easiest way is to use the snippet below.
/* Reading the file name and the book (UTF-8) */
if (file_exists($SourceDirectoryFile))
{
$NameBook = "name.txt";
$AboutBook = "about.txt";
// Using file_get_contents instead. Less code
$myFile = "Computer-Technolgy/2 ($i)/" . $NameBook;
$contents = file_get_contents($myFile);
// I want my browser to display UTF-8 characters
header('Content-Type: text/html; charset=UTF-8');
echo 'Name file: ' . $contents . '<hr/>';
}
Please note that the header function needs to be executed at the beginning of the output to the browser. So for instance if you have additional data that is displayed prior to this function, you need to move the header statement at the top. Otherwise you will end up with warnings on screen that the headers have already been set.

You'll need to make sure that the page where you're displaying the text file has correct encoding.

final and best solution is this:
use this line under your connect
mysqli_set_charset( $con, 'utf8');
like this:
$con = mysqli_connect("localhost","root","amirahmad","shoutit");
mysqli_set_charset( $con, 'utf8');
and at the end add this line right under the head tag in your html to make sure your page have utf-8 charset,like this:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and that's it . you can read formal document here : pph.net charset

Related

fpassthru acting weird (sending the page html rather then the file I want)

I'm really really new to PHP, so if you can explain it to me what my code is actually doing and why the result is what it is I would appreciate very much. I'm probably screwing up something very simple.
Basically I want to query a MySQL database, create a csv with the data, and download the csv. Pretty simple. Here is my code:
<?php
include("Includes/PHPheader.php");
$query_string = $_SERVER['QUERY_STRING'];
parse_str($query_string);
$sql = "SELECT many_columns_i_removed_from_this_sample_code FROM table WHERE id = '".$id."'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$f = fopen("csv/tmp.csv", "w");
fputcsv($f, array_keys($row),';');
fputcsv($f,$row,';');
rewind($f);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="tmp.csv"');
fpassthru($f);
fclose($f);
?>
There are some HTML code below it that shouldn't affect anything, but just in case here it is.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
</body>
</html>
Well, I thought this would download my csv with no problem. If I go to the csv folder, there it is, the tmp.csv file I created, with the proper header and data.
But when I open the tmp.csv file I downloaded, it is actually the html code of the page, and not the data I expected.
What is going on?
In case it helps, I'm using WebMatrix 3.0.
There are two things going on, probably. First, You are trying to read (fpassthru) from a file opened for writing (fopen(..., "w")), so You are not able to read anything from the file. Then, after that "reading nothing" goes Your HTML code, which naturally appends to Your output. Try this:
$f = fopen("csv/tmp.csv", "w+");
...
fclose($f);
exit;
Could you please try?
header("Content-type: application/vnd.ms-excel");
instead of
header('Content-Type: application/csv');
I have a test code of CSV output, have a look - https://foowms.googlecode.com/svn/trunk/stockincsv.php
A very informative thread of Stack Overflow
Setting mime type for excel document
==============================================
If csv file open, write and save, then you should do as follows-
$list = array ("Peter,Griffin,Oslo,Norway");
$file = fopen("csv/tmp.csv","w");
foreach ($list as $line) {
fputcsv($file,explode(',',$line));
}
fclose($file);
==============================================
You also could try this
fputcsv($fp, array_values($list), ';', ' ');
instead of
fputcsv($f, array_keys($row),';');

how to write a string to a file that contains html tags

I have created new .php file through php file system and now I am trying to write html code into file. Here is the code
$file = 'django unchained.php';
if($handle = fopen($file , 'a'))
{
$text = htmlentities("Just link");
echo $text; // here it shows up fine in webpage.
file_put_contents($file , $text); // after writing to file it doesnot appear the same in .php file
fclose($handle);
}
As mentioned in comment above when I echo it in webpage it shows up fine but doesnot appear the same when same string is written to file.
Thanks in advance.
If you open the source code of your HTML page, you'll see the same text, you see in django unchained.php:
<a href="index.php">Just link</a>
This text is generated by htmlentities function, that converts special HTML characters to their entity equivalents. This behaviour is absolutely predictable and correct.
If you want to output to file the unquoted text, do this:
<?php
$file = 'django unchained.php';
$text = "Just link";
echo htmlentities($text); // here it shows up fine in webpage.
file_put_contents($file , $text) or die('Error opening file');
Looks like you have problems with locks, try this code:
$file = 'django unchained.php';
$text = "Just link";
file_put_contents($file , $text) or die('Cannot open file for writing');
note: you do not need fopen, fclose, htmlspecialchars here

Php writing edited script file content cause extra line breaks

In a multilingual site i have two php files that contains php constants.
Like
define('EMAIL', 'Email');
define('GENDER', 'Gender');
.
.
.
I provide editing of these files from admin side using a textarea in form. print full file in textarea.
When ever admin Update the files it contribute redirection issue, means after inclusion of this file header() function fails reporting a non white space character above.
I checked the php file after editing, and it contain a lot of extra space between each php statment as follow,
define('EMAIL', 'Email');
define('GENDER', 'Gender');
define('NAME', 'name');
Also a long single line breaks into many lines like.
define('SENTENCE', 'this is a long sentence that
breaks into many lines according to width of text area as i noted');
So this also contribute error as it must be in single line
I am sure these extra spaces and line breaks are cause of all issues. I am using this code in printing between textarea:
<textarea style="width: 664px; height: 353px;" id="edit_file" name="edit_file"><?php
$file = fopen("../en.php", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file);
}
fclose($file);
?> </textarea>
and for saving file:
if(isset($_POST['btn']) && $_SERVER['REQUEST_METHOD']=='POST' && !empty($_POST['btn'])){
if (get_magic_quotes_gpc()) {
$filedata = stripslashes($_POST['edit_file']);
}
$filedata=str_replace(array("<br />'",'\n'),array("",''),$filedata);
$size=strlen($filedata);
$file = fopen("../en.php", "w") or exit("Unable to open file!");
fwrite($file,"$filedata",$size);
fclose($file);
}
There is 1 unexpected quote and \n cannot be put inside simple quotes :
$filedata=str_replace(array("<br />'",'\n'),array("",''),$filedata);
Replace by :
$filedata=str_replace(array("<br />","\n"),array("",''),$filedata);

Character encoding issues - UTF-8 / Issue while transmitting data on the internet?

I've got data being sent from a client side which is sending it like this:
// $booktitle = "Comí habitación bailé"
$xml_obj = new DOMDocument('1.0', 'utf-8');
// node created with booktitle and added to xml_obj
// NO htmlentities / other transformations done
$returnHeader = drupal_http_request($url, $headers = array("Content-Type: text/xml; charset=utf-8"), $method = 'POST', $data = $xml_data, $retry = 3);
When I receive it at my end (via that drupal_http_request) and I do htmlentities on it, I get the following:
Comí habitación bailé
Which when displayed looks like gibberish:
Comí Habitación Bailé
What is going wrong?
Edit 1)
<?php
$title = "Comí habitación bailé";
echo "title=$title\n";
echo 'encoding is '.mb_detect_encoding($title);
$heutf8 = htmlentities($title, ENT_COMPAT, "UTF-8");
echo "heutf8=$heutf8\n";
?>
Running this test script on a Windows machine and redirecting to a file shows:
title=Comí habitación bailé
encoding is UTF-8heutf8=
Running this on a linux system:
title=Comí habitación bailé
encoding is UTF-8PHP Warning: htmlentities(): Invalid multibyte sequence in argument in /home/testaccount/public_html/test2.php on line 5
heutf8=
I think you shouldn't encode the entities with htmlentities just for outputting it correctly (you should as stated in the comments use htmlspecialchars to avoid cross side scripting) , just set the correct headers and meta end echo the values normally:
<?php
header ('Content-type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
</body>
</html>
htmlentities interprets its input as ISO-8859-1 by default; are you passing UTF-8 for the charset parameter?
Try passing headers information in a key/value array format.
Something like
$headers = array("Content-Type" => "text/xml; charset=utf-8"")

Output text file with line breaks in PHP

I'm trying to open a text file and output its contents with the code below. The text file includes line breaks but when I echo the file its unformatted. How do I fix this?
Thanks.
<html>
<head>
</head>
<body>
$fh = fopen("filename.txt", 'r');
$pageText = fread($fh, 25000);
echo $pageText;
</body>
</html>
To convert the plain text line breaks to html line breaks, try this:
$fh = fopen("filename.txt", 'r');
$pageText = fread($fh, 25000);
echo nl2br($pageText);
Note the nl2br function wrapping the text.
One line of code:
echo nl2br( file_get_contents('file.txt') );
If you just want to show the output of the file within the HTML code formatted the same way it is in the text file you can wrap your echo statement with a pair of pre tags:
echo "<pre>" . $pageText . "</pre>;
Some of the other answers look promising depending on what you are trying todo.
For simple reads like this, I'd do something like this:
$fileContent = file_get_contents("filename.txt");
echo str_replace("\n","<br>",$fileContent);
This will take care of carriage return and output the text. Unless I'm writing to a file, I don't use fopen and related functions.
Hope this helps.
Before the echo, be sure to include
header('Content-Type: text/plain');
Are you outputting to HTML or plain text? If HTML try adding a <br> at the end of each line. e.g.
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // Read a line.
echo "$buffer<br/>";
}
Trying to get line breaks to work reading a .txt file on Apache2 and PHP 5.3.3 with MacOSX 10.6.6 and Camino, the echo nl2br( $text); didn't work right until I printed the file size first too.
BTW it doesn't seem to matter if the .txt file has Linux/MacOSX LF or Windows CRLF line breaks or the text encoding is UTF-8 or Windows Latin1, Camino gets it out OK.
<?php
$filename = "/Users/Shared/Copies/refrain.txt";
$file_ptr = fopen ( $filename, "r" );
$file_size = filesize ( $filename );
$text = fread ( $file_ptr, $file_size );
fclose ( $file_ptr );
echo ( "File size : $file_size bytes<br> <br>" );
echo nl2br ( $text );
?>
You need to wrap your PHP code into <?php <YOU CODE HERE >?>, and save it as .php or .php5 (depends on your apache set up).
Say you have an index.php file hosted by the web server. You want to insert some multi-line text file contents into it. That's how you do it:
<body>
<div>Some multi-line message below:</div>
<div><?= nl2br(file_get_contents('message.txt.asc')); ?></div>
</body>
This <?= ... ?> part is just a shorthand, which instructs the web server, that it needs to be treated as a PHP echo argument.

Categories