I am trying to input some Vietnamese language from text box to my file then read from that file and display in another page.
The display part is working well as I tried to copy, paste some Vietnamese directly to file and test the displaying. However the writing part some how not right, because when I try input some Vietnamese and test at the display, it will miss some characters at some places. Here is the code I am using to input to file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form name="form" method="post">
<style type="text/css">
.inputtext { width: 550px; height: 550px; }
</style>
<input type="text" name="text_box" class="inputtext" size="250"/>
<input type="submit" id="search-submit" value="SAVE" />
</form>
</body>
</html>
<?php
if(isset($_POST['text_box'])) { //only do file operations when appropriate
$a = $_POST['text_box'];
$myFile = mb_convert_encoding("test.txt", "UTF-8", "auto");
$data = mb_convert_encoding($a, 'UTF-8', "auto");
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh,utf8_encode($data));
fclose($fh);
}
?>
So how is the right way to write UTF8 (or any multi language) to file ?
If you make sure all your pages are already using UTF-8, then the solution would be: Do nothing special with the file, simply write the string (which already is UTF-8) to the file.
So now you need to find out how to make everything in your page UTF-8. You should start sending a Content-type header: header('Content-type: text/html; charset=utf-8');
Related
I have a string that I am forcing download of as txt file on submit of a form, but it adds current page's HTML before and after the string in the txt file.
PHP:
if ( isset( $_POST['txt_submit'] ) ) {
$arr = array('abc#abc.com', 'xyz#xyz.com');
$string = implode(PHP_EOL, $arr);
header ('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=emails.txt');
echo $string;
}
The HTML is not random, it is existent on the page on which I am running this PHP code.
The txt that downloads looks like this:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div>
...
</div>
abc#abc.com
xyz#xyz.com
<form action="" method="post">
<input type="submit" value="Generate txt file" name="txt_submit">
</form>
</body>
</html>
I can't put my PHP code before everything else because I am doing this on Wordpress in an admin menu page of a custom plugin, so this code is already inside the admin_menu action hook.
You need to put the download before everything else, and use exit(); after the echo, to prevent HTML from being in output.
I'm trying to get text from a pdf file with Hebrew in it and manipulate it, but when I'm using echo it shows these letters instead of Hebrew:
Ço̬mÀÃ6ÜÍzWÃýCW¶°ÐÞ]Aµ±¸¤:ÄÞ[JÞaCå+wÎ[n6GZù>"âÊù+ýÕ9^6ÓF½íoßEcì¸_pùnÚbïjÅÅß^UtýÝ-®»þgåĿٻƷ8ԯβzÅr
I made sure the page is in utf-8 and converted the returned text to utf-8 but it doesn't fix it.
When The text wasn't in utf-8 it showed these symbols:
��G�W����/��<� ������%�M����>����z.�m47�M �O�4�Nf�/7ʓ쓻#2FGj��,U8�J
I feel like I'm just missing something.
This is my code:
<?php
header('Content-type: text/html; charset=UTF-8');
$formReturn = $_POST["formReturn"];
if ($formReturn)
{
$file = $_FILES["gradesPdf"]["tmp_name"];
$text = file_get_contents($file);
$text = utf8_encode($text);
}
$html = '
<!DOCTYPE html>
<html lang="he">
<meta charset="utf-8" />
<head>
<title>נסיון</title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<input type="file" name="gradesPdf" id="gradesPdf">
<br><br>
<button type="submit">run</button>
<input type="hidden" name="formReturn" value="1">
</form>
'. $text .'
</body>
</html>
';
echo $html;
Btw I can't use pdfParser, I tried the demo on their site and it didn't return the text the way I wanted. I think since my pdf has a table in it.
I'm receiving a text file upload and then just printing it back out. For some reason, special characters are appearing as black boxes with a white check mark. I tried htmlentities() and utf8_encode() on the content to be print to screen, but that didn't help.
Here's all of my code:
<?php ini_set("auto_detect_line_endings", true);
header('Content-Type: text/html; charset=utf-8');
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body style="overflow:visible;">
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="upload" value="upload">Upload</button>
</form>
<pre>
<?php
if($_POST['upload']) {
//$fileName = 'old.txt';
$fileName = $_FILES['file']['tmp_name'];
if(file_exists($fileName)) {
$file = fopen($fileName,'r');
while(!feof($file)) {
$name = fgets($file);
echo(htmlentities($name));
}
fclose($file);
}
}
?>
</pre>
</body>
</html>
This code works on my localhost LAMP server, but the character probelm appears on some other people's servers. What can I do to maek the special characters show up?
My code for testing is as follows:
Both files are .php to avoid conflict, I have been programming for 30 years, but am new to .js and .php, I can't figure out the syntax for what should be an easy effort. I have read and tried all applicable examples but they haven't worked for me. PLEASE TELL ME WHERE I'M SCREWING UP!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestofTopicText</title>
<script language="Javascript">
<!--
function OnButton1()
{
var newtopic = document.getElementById('topic');
document.Form1.target = "_self";
document.Form1.action = "1-open-close.php?var=$newtopic";
document.Form1.submit(); // Submit the page
}
-->
</script>
</head>
<body>
<h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here! </span></h3>
// Using get method as I read was appropriate for getElementById
<form id="Form1" method="get" name="Form1">
<input type="text" name="q" id="topic" size="55" />
<input type="submit" name="sa" value="Search" onclick="OnButton1()"/>
</form>
</body>
</html>
// I am passing to this .php file known as 1-open-close.php
// The file opens and writes test text but I can't get topic text from other file?
<?php
$topic = $_GET['var'];
$myFile = "Topics.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Test to Make Sure Open \n";
fwrite($fh, $topic);
fwrite($fh, $stringData);
fclose($fh);
// return true;
?>
Not sure if you are really looking to involve javascript in this, but you could simplify by just using the form behavior native to html, and reference the input passed in. i.e. You've already specified the form as method GET, so the input type text contained within the form tags will be posted without any extra effort. On the php side, you can reference that value coming through by using the "name" specified on the html input as the index to the array. Hope this helps!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestofTopicText</title>
</head>
<body>
<h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here! </span></h3>
// Using get method as I read was appropriate for getElementById
<form id="Form1" method="get" name="Form1" action="1-open-close.php">
<input type="text" name="q" id="topic" size="55" />
<input type="submit" name="sa" value="Search"/>
</form>
</body>
</html>
// I am passing to this .php file known as 1-open-close.php
// The file opens and writes test text but I can't get topic text from other file?
<?php
$topic = $_GET['q'];
$myFile = "Topics.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Test to Make Sure Open \n";
fwrite($fh, $topic);
fwrite($fh, $stringData);
fclose($fh);
// return true;
?>
JavaScript doesn't interpolate variables like PHP does, so it is literally sending the string $newtopic instead of what you want. The whole thing is redundant anyway. Try this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TestofTopicText</title>
</head>
<body>
<h3 style="color:#0f0;">If NOT found to right ENTER your topic here!</h3>
<form method="post" action="1-open-close.php">
<input type="text" name="q" size="55" />
<input type="submit" value="Search" />
</form>
</body>
</html>
Then your PHP file should be:
<?php
$topic = $_POST['q'];
$myFile = "Topics.txt";
$fh = fopen($myFile,"a") or die("can't open file");
$stringData = "Test to make sure open\n";
fwrite($fh,$topic);
fwrite($fh,$stringData);
fclose($fh);
?>
EDIT: I see you put your form action into your javascript. That's weird. Anyway:
I suggest you use a LOT of echo statements to find out what variables' values are.
Useful things:
print_r($_POST); // find out what values have been received by POST method
print_r($_SESSION); // find out what values are stored in SESSION
Surround those print_r with <pre></pre> tags to make them easily readable.
Here:
var newtopic = document.getElementById('topic');
document.Form1.target = "_self";
document.Form1.action = "1-open-close.php?var=$newtopic";
You are trying to pass the JavaScript variable newtopic with $newtopic (which is the syntax for PHP variables, and does not apply here).
And you are also trying to send the element itself, which does not make sense.
The input box has a name="q" and that is what you should be accessing in PHP.
Nevertheless the code is still badly formed. You will get errors from trying to access "q" before it is sent.
I've added an action to the form, and deleted your script which was not necessary. I've also changed $topic = $_GET['var'] to $topic = $_GET['q'];
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>TestofTopicText</title>
</head>
<body>
<h3><span style="color: #00ff00;">If NOT found to right ENTER Your Topic Here! </span></h3>
// Using get method as I read was appropriate for getElementById
<form id="Form1" method="get" name="Form1" action="1-open-close.php">
<input type="text" name="q" id="topic" size="55" />
<input type="submit" name="sa" value="Search" />
</form>
</body>
</html>
// I am passing to this .php file known as 1-open-close.php
// The file opens and writes test text but I can't get topic text from other file?
<?php
$topic = $_GET['q'];
$myFile = "Topics.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "Test to Make Sure Open \n";
fwrite($fh, $topic);
fwrite($fh, $stringData);
fclose($fh);
// return true;
?>
I'm trying to replace some non standard characters like ë,Ë,ç,Ç with numeric entities like Ë , ' etc but i ran into a bit of a problem.
When i try to replace them directly like this it works fine:
$string = "Ë";
$vname = str_replace("Ë","AAAA",$string);
echo $vname."<br>";
an i get AAAA as a result.
But when i try to replace the characters from a string that i get from a form with POST then it doesn't change the characters. Here is an example:
<?php
if(isset($_POST['submit'])) {
$string = $_POST['title'];
if ($string == "Ë")
echo "Yes";
else
echo "No";
$vname = str_replace("Ë","AAAA",$string);
echo $vname."<br>";
echo $string;
}
?>
<form method="post" name="Form">
Title: <input name="title" type="text" value="" size="20"/>
<input name="submit" type="submit" value="submit"/>
</form>
Any help would be great!!
Most likely your characterset is wrong. I would suggest sending the following header when outputing html:
<?php header("content-type: text/html; charset=utf-8"); ?>
Where the charset match the charset you are storing your file in.
Edit: Just some more information. The file you store is in one charset for example latin1, while your browser interprets your html page as another charset (utf-8 for example). When the browser then sends the Ë character, it will send the utf-8 code 0xc38b, while the same character is 0xcb. As you can see, these does not match.
Edit - You can also update the CHARSET via HTML5 or xHTML:
HTML5
<meta charset="UTF-8"/>
xHTML
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />