Why php includes doesn´t processes accents right? - php

As explained here (PHP Include and accents (They show up as �)) php has a strange behavior processing the accents. My question is Why?
I mean: I have a simple utf-8 charset page. With this:
<!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>
<title>My Title</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php include ('file.php');?>
The included file just says: "Administración." It has no charset, just header tags (h2, h3...), and some links. Like this:
<h2>Administración</h2>
So, there is no charset conflict. Are not they supposed that the include files are just included?
The previous question was answered with some fix to the problem, but my question is Why PHP behaves this way?

to answer you new questions (from your comment):
How can I do the same thing in other
editors, how do I know the default
charset?
default-charset and charset for every single file can be set in almost every code-editor i know - where exactly depends on the editor. simply take a look into the manual/documentation of your editor for that.

Related

Mail template encoding suddenly wrong. Only rewrite manually works it seems. Some tips, knowledge?

I have a problem with our E-Mail templates.
They worked fine until 2-3 days ago i have no idea what happened.
They should be utf8 encoded and get shown correctly in PHP Storm they look something like this:
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
....
}
</style
</head>
<body>
<center>Content with unique characters like ěäüß</center>
</body>
</html>
But in the Email the output is completely rubbish:
Vaše zakázka etc.
I use the output buffer to get the template content and send it with PHPMailer.
There is no encoding anywhere but shouldn't be needed.
I tried utf8-encode/decode etc. It makes everything only worse.
The only thing that worked is to use tools like this http://www.percederberg.net/tools/text_converter.html
to convert the plain text into utf-8 and then the code looks like rubbish like in the mail output, but then you can manually correct every character.. that worked for some templates like in german with some äüß.... but in the Czech template you have to rewrite every single character.
Is there something that i missed? Something that i could try?
I used htmlentities() on the text parts of the templates and worked quite decent!
Example:
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
....
}
</style
</head>
<body>
<center><?php echo htmlentities("Content with unique characters like ěäüß"); ?></center>
</body>
Hope this helps someone!

Changing the default dreamweaver templates

I'm not sure if this is programming-related enough for stack overflow, but anyways...
I want to change the code that is created in dreamweaver when you create a new HTML file or PHP file.
For example, I want to replace the default doctype and HTML tag:
<!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">
with this:
<!DOCTYPE html>
<html>
How can I do this?
On Mac OS X, you'll find Dreamweaver's Document Type Declaration configurations at:
/Applications/Adobe Dreamweaver/Configuration/DocumentTypes
To add a new template for HTML5 documents, open the file MMDocumentTypeDeclarations.xml and add a new element:
<documenttypedeclaration id="mm_html_5">
<title>
<MMString:loadString id="mmdocumenttypedeclarations/mm_html_5" />
</title>
<doctypedecl>
<![CDATA[<!DOCTYPE HTML>]]>
</doctypedecl>
<rootelement>
<![CDATA[<html></html>]]>
</rootelement>
<dtdcontext>html</dtdcontext>
<dtdcontext>html5</dtdcontext>
<dtdcontext>frameset_frame</dtdcontext>
<dtdcontext>xslt</dtdcontext>
</documenttypedeclaration>
When creating new documents, Dreamweaver references MMDocumentTypeDeclarations.xml and matches the <dtdcontext> against the Page Type you selected in the New Document dialog box.
By modifying this XML file, you don't have to edit any of the templates in:
/Applications/Adobe Dreamweaver/Configuration/DocumentTypes/NewDocuments
If you're running Dreamweaver CS5+ (version 11), Adobe already includes updated DTDs for HTML5:
Which creates the following blank document:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
You can change which DOCTYPE is used by default when creating new documents by going into Dreamweaver's preferences:
In Windows, the file is at
C:\Program Files\Adobe\Adobe Dreamweaver CS3\configuration\DocumentTypes\NewDocuments\Default.html
Replace "CS3" with your Adobe CS version.
Still works in 2019 (using the subscription) in Win10:
C:\Program Files\Adobe\Adobe Dreamweaver CC 2019\configuration\DocumentTypes\NewDocuments

How do I display foreign languages in my php file

When I try to display letters of a foreign languages in my php file they show up as "?".
Anybody have any ideas how I can display them properly?
If I save the file as a HTML it displays the letter properly.
To add support for foreign characters you need to use UTF-8 (or any other similar encoding but UTF-8 is the most widely used) encoding.
text editing
At first you need to have your PHP/HTML files in UTF-8 encoding, use a text editor that supports this encoding, just check that the editor doesn't prepend UTF-8 BOM symbols to the file.
PHP
To serve your PHP files as UTF-8 (so the browser doesn't get mixed up about this) add relevant header
<?php header("Content-Type: text/html; Charset=UTF-8"); ?>
HTML
To serve your static HTML pages as UTF-8 use appropriate meta tag
<!-- with HTML5 -->
<meta charset="utf-8" />
<!-- or with older HTML formats -->
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
MySQL
To save/load UTF-8 encoded text in MySQL use the following statement after connecting to the MySQL server
<?php
mysql_connect(...);
mysql_set_charset('utf8');
?>
but check that the tables have also appropriate encodings set
+Using charset=utf-8
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
</head>
<body>
<body>
</body>
++ If you mention about open your php file .you have use an editor that support utf-8 something like this.
I am using Eclipse .

Header says UTF-8 but accents not showing up properly - why? (php)

I abstracted the header from a larger set of php files for clarity. When I load it into Wampserver, the <p>é</p> appears as � on the site, despite the header calling for utf-8 charset. What is wrong in this document?
(Note that I tried to modify the encoding by replacing iso-8859-1 with utf-8, that didn't help.)
header.php:
<?php
header('Content-Type:text/html; charset=UTF-8');
echo '<?xml version="1.0" encoding="iso-8859-1"?>'
?>
<!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" xml:lang="fr" lang="fr">
<head>
<title>Blabla</title>
</head>
<body>
<p>é</p>
</body>
</html>
try this<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> in the head section
and also check your file encoding
You are sending two contradicting character sets, iso-8859-1 and utf-8.
If you
fix that and send only one character set, and
encode the actual file in the character set you specify (there should be a character set option in your IDE's or editor's "Save as..." dialog)
it should work.
this worked for me :
I add to the MVC COntroller : produces={"application/json;charset=utf-8"}

php include not working on IE?

This include is not working in IE:
<?php
include_once 'localization.php';
?>
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Global Colleague</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/><!--Start Kampyle Exit-Popup Code-->
<script type="text/javascript">
Calling an array inside localization.php
<?php echo l('content_p3'); ?>
EDIT: I tried the same file in another folder and it worked
"Not working" is very generic. What is it that you expect and what is actually happening? You may want to turn error reporting on and see if any error is reported. In (X)HTML, nothing should be printed before the doctype. Are you trying to add something to the HTTP response? Typically, the browser shouldn't effect how PHP outputs your code unless you've added some code to respond to the user agent which is not always wise.
Perhaps, when you tried your code in another directory it wasn't able to find the offending script as it is included by a relative path. Try removing the include in the original file and see if it "works".
The following way 100% work for you:
1) Install (Open Source) Notepad++ On your PC
2) Open the file in it
3) Go to encoding and select (Encode in UTF-8 Without BOM)
4) Then click save
5) Now it can work on (Chrome) and (IE)
:)

Categories