I have a script for real estate, it contains a locale file for translation, i translated it but the text displayed as question marks... tried the following with no success
adding
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
in head element
adding AddDefaultCharset utf-8 to .htaccess
adding AddDefaultCharset utf-8 to httpd.ini
adding this lines to php.ini
default_charset = "utf-8"
mbstring.internal_encoding=utf-8
mbstring.http_output=UTF-8
mbstring.encoding_translation=On
mbstring.func_overload=6
here are some script pages:
index.php (control panel, here the translated text is also garbled)
<?php
ob_start();
?>
<!doctype html>
<html>
<head>
<title>Property Listing</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
{APP_TPL}
</body>
</html>
<?php include 'listings.php'; ?>
Listing.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<?php
if (!isset($_GET['iframe']))
{
$content = ob_get_contents();
ob_end_clean();
ob_start();
}
if (!isset($_GET['controller']) || empty($_GET['controller']))
{
$_GET["controller"] = "Listings";
}
if (!isset($_GET['action']) || empty($_GET['action']))
{
$_GET["action"] = "index"
include dirname(__FILE__) . '/index.php';
if (!isset($_GET['iframe']))
{
$app = ob_get_contents();
ob_end_clean();
$app = str_replace('$','$',$app);
echo preg_replace('/\{APP_TPL\}/', $app, $content);
}
?>
preview (the main visitor page, all the text garbled)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<?php
ob_start();
?>
<!doctype html>
<html>
<head>
<title>Property Listing</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
{APP_TPL}
</body>
</html>
<?php include 'listings.php'; ?>
en.php (locale file)
#Login
$PL_LANG['login_login'] = 'Admin login';
$PL_LANG['login_username'] = "المستخدم";
$PL_LANG['login_password'] = "كلمة السر";
$PL_LANG['login_login'] = "Admin Login";
$PL_LANG['login_register'] = "تسجيل";
$PL_LANG['login_err'][1] = "Wrong username or password";
$PL_LANG['login_err'][2] = "Access denied";
$PL_LANG['login_err'][3] = "Account is disabled";
$PL_LANG['login_error'] = "Error";
# Left menu
$PL_LANG['menu_home'] = "Home";
$PL_LANG['menu_properties'] = "Properties";
$PL_LANG['menu_options'] = "Options";
$PL_LANG['menu_install'] = "Install";
$PL_LANG['menu_preview'] = "Preview";
$PL_LANG['menu_logout'] = "Logout";
$PL_LANG['menu_users'] = "Users";
# General
$PL_LANG['_yesno']['T'] = "Yes";
$PL_LANG['_yesno']['F'] = "No";
Make sure the editor you are using to save those files is writing the file as UTF-8.
You can also try converting the text to UTF-8
$utf8_text = mb_convert_encoding($non_utf8, 'UTF-8', mb_detect_encoding($non_utf8));
Related
I have created a simple webpage, which includes both a header and footer as separate php files, shown below
<?php
$PageName = "Home Page";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
this is the header
<?php
print("<!DOCTYPE html>");
print("<html lang='en-UK'>");
print("<head>");
print("<title>");
print($PageName);
print("");
print("</title>");
print("<meta http-equiv='content-type' content='text/html; charset=utf-8' >");
print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
$CSSRoot = "/MyPage/StyleDefault.css";
print("<link rel='stylesheet' type='text/css' href=$CSSRoot>");
print("</head>");
print("<body>");
print("<h1>My Page</h1>");?>
and footer
<?php print("</body></html>");?>
but when I view it the header elements appear in the body as shown below
header information appearing in the body
I want to make clear this does not, yet, cause any problems, but I want to know what the cause is.
Thanks
EDIT
brain fart moment putting the code in the comments, sorry.
new index
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
new header
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
new footer
</body></html>
new output
<html lang="en-UK"><head></head><body>
<title>Home Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/MyPage/StyleDefault.css">
<h1>My Page</h1>
</body></html>
this is how you should do this.
header.php
<!DOCTYPE html>
<html lang="en-UK">
<head>
<title><?php echo $PageName;?></title>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<link rel='stylesheet' type='text/css' href="<?php echo $CSSRoot;?>">
</head>
<body>
index.php
<?php
$PageName = "Home Page";
$CSSRoot = "/MyPage/StyleDefault.css";
include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/header.php";
?>
<h1>My Page</h1>
<?php include $_SERVER['DOCUMENT_ROOT'] . "/MyPage/footer.php";?>
footer.php
</body>
</html>
This should work, I don't know why you would want to print all the html tags using php, while you can just output em normal.
I beleive I have found the problem, the sources panel in chrome shows that just before the doctype and end body tags are two characters (ie where the header and footer files begin), represented by red dots, according to http://apps.timwhitlock.info/unicode/inspect?s=%EF%BB%BF%EF%BB%BF this is a ZERO WIDTH NO-BREAK SPACE, I don't know how they appeared and I can't seem to get rid of them, but it seems likely that they are the cause of the problem.
UPDATE
having looked around the prolbem is that the php files where using UFT-8 BOM (byte order mark) which inserted the offending characters, changing it to UTF-8 (which is under encoding in Notepadd++) solved the problem, thanks for all the help
I have a full HTML page:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>
I'm trying to save it in a variable like so:
$template = htmlentities("<!DOCTYPE HTML><html lang="en-US">...", ENT_HTML5, "UTF-8" );
.. but it chokes at just the first HTML tag.
That's because the first HTML tag has double quotes, just like you use for delimiting your string literal.
$template = <<<EOD
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>
EOD;
You are not escaping the string propertly Try to:
Replace
htmlentities("//whatever your html code is//");
with
htmlentities('//whatever your html code is//');
user addslashes function..it will not truncate your string in between.
This function can be used to prepare a string for storage in a database and database queries.
Before storing into database or for any purpose
$final_string = addslashes('<!DOCTYPE HTML>
..........');
Before rendering that output on browser
$normal_string = stripslashes($database_retrived_string);
$data = '<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Template</title>
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
...
</head>
<body>
...
</body>
</html>';
base64_encode($data);
Try this:
$temp = addslashes('<!DOCTYPE HTML><html lang="en-US">...', ENT_HTML5, "UTF-8" );
$template = htmlentities($temp);
I'm developing a php web application using Dreamweaver CS5. When I create a new page, Dreamweaver automatically adds the following code
<!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>Untitled Document</title>
</head>
<body>
</body>
</html>
What I'm trying to do is to take this code and put it in a file, say 'head.php', and then I want to import this head.php file to all the pages I create using
<?php include("head.php"); ?>
If I do this, it means that I'm gonna have the same page title for every page because the code <title>Untitled Document</title> is included in head.php.
So is there a way for me to send the page title via a variable from the new pages I create and then set it on the head.php document. So if I had a customer.php page, it would look something like this.
$pageTitle = "Customer List";
<?php include("head.php"); ?>
Then pass $pageTitle to the the head.php
Inside your header.php
function header($title)
{
echo '<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.$title.</title>
</head>';
}
then you include your head.php
< ?php include("head.php"); ?>
and finally, instead the < head >< /head > tags in your main file your call the function to create the title.
<?php header('This is my Awesome Page'); ?>
Or still
<?php
$pageTitle = "My Title";
header($pageTitle);
?>
This should work for you.
FUNCTIONS.PHP
<?php
function global_header($page)
{
echo "
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>" . $page . "</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>
";
}
?>
<?php
function global_footer()
{
echo "
</body>
</html>
";
}
?>
When I view my page source in chrome/FF I get the following source:
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Add</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>
</body>
</html>
It's indented by about 3 tabs. Is there a PHP strip function or something that can align it properly? I don't like my entire pages HTML being messed up.
My expected output is to not be indented.
The reason you are getting indented outputs is that you are echoing them like that...
Simply remove the indentaions from the echo statements to get rid of them
<?php
function global_header($page)
{
echo "
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>" . $page . "</title>
<meta name='description' content='BTI320 Assignment 2' />
</head>
<body>";
}
?>
<?php
function global_footer()
{
echo "
</body>
</html>";
}
?>
This makes your php harder to follow fut the output will be as you requested
Consider using a template engine. Direct output of HTML strings is considered bad practice.
If you don't want to use third-party template engines, you can anyway benefit from some simplified templating like this:
page.tpl template file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<title>{{title}}</title>
</head>
<body>
{{body}}
</body>
</html>
PHP:
// Loading HTML code that does not contain any undesired whitespace.
$code = file_get_contents('page.tpl');
// Replacing template variables with their values.
$code = str_replace(
array(
'{{title}}',
'{{body}}'
),
array(
'Example title',
'Page body'
),
$code
);
// Outputting resulting HTML code.
echo $code;
I want to make a transition page, to forward "URL" to. I have this code. Self explanatory. !var means that some var is not given. URL is some url like http://domain.com
session_start();
if (!$some_variable) {
$data = "http://localhost/";
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
transition.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title>Frameset</title>
</head>
<frameset rows="50%,50%">
<frame src="above.php" name="Navigation">
<frame src="http://www.domain.com" name="Daten">
<noframes>
<body>
<p>Something</p>
</body>
</noframes>
</frameset>
</html>
above.php:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>
</title>
</head>
<body>
<div style="text-align: right;">Continue
</div>
</body>
</html>
Which leads to
<a href="">
Why is that so?
It's always best to give full code and not use !var - just put in the correct variable here as it is less confusing. Secondly you don't have any session in above.php
Unknown file:
<?php
session_start();
if (!isset($_SESSION['keks'])) {
//header("Location: ".URL, true, 301);
$_SESSION['keks'] = "http://localhost/";
require("transition.php");
exit();
}
?>
above.php
<?php
session_start();
?>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
</head>
<body>
<div style="text-align: right;">
Continue
</div>
</body>
</html>
This should work:
session_start();
if (!isset($_SESSION['keks'])) {
$data = 'URL';
$_SESSION['keks'] = $data;
require("transition.php");
exit();
}
Also in above.php you need
<?php session_start(); ?>
To get the variable.