I wrote following code for my page with utf-8 charset command:
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
<title> . . . </title>
<style type="text/css";>
<!--
.mystyle {
font-size:12px;
}
-->
</style>
<?php
$myDirectory = opendir(".");
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
closedir($myDirectory);
$indexCount = count($dirArray)-2;
print("<table border=0 cellpadding=3 cellspacing=0 class='mystyle'>\n");
print("<tr> . . . </tr>\n");
.
.
.
print("<td align=right>");
print(number_format(filesize($dirArray[$index])/1024));
}
.
.
.
print("<td><a href=\"$dirArray[$index]\">
.
.
.
}
print("</table>\n");
?>
When I run this code, it is run in correct mode, but with writing files in a list, it doesn't work properly in charset="utf-8" mode. How can I add charset in php part?
Setting the charset in php is made like this :
header("Content-Type: text/html; charset=UTF-8") ;
NB It should be sent before to write in the output, so, before your <meta http-equiv="Content-Type" content="text/html; charset="utf-8">.
<?php
header("Content-Type: text/html; charset=UTF-8") ;
?><!doctype html><html>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
...
Related
I'm trying to create a function 'header' which will print html content (doctype, html, head, body, etc) - but when I'm looking in a site source, all of that stuff is in one line, not in a tree hirearchy...
public function header() {
print(
'<!DOCTYPE HTML>'
. '<html>'
. '<head>'
. '<meta charset="utf-8"/>'
);
And when I'm looking in the web source the output looks like:
<!DOCTYPE HTML><html><head><meta charset="utf-8"/>
I would like it to look more like standard html tree:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
How can I do that? What are the options ?
EDIT:
Some of You showed me an echo option - it works, but it looks really bad in a php file - like:
public function header() {
echo "<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
</body>
</html>
";
The most classic way, using echo :
echo '<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
';
see below methods for printing HTML inside PHP code block
FOR SHORT HTML CONTENTS
echo ' <div class="myClass"> Some Text here. Some text here </p> ';
FOR SHORT HTML CONTENTS WITH PHP variable concatenation
$myName='Optimum';
echo ' <div class="myClass"> My Name is '. $myName .' </p> ';
FOR LONG CONTENT
$html.='';
$phpVariable ='Dummy content'
$html.='<div class="wrapper">'
$html.='<div class="content">';
$html.='<p> My content here'. $phpVariable .' </p>';
$html.='</div>';
$html.='</div>';
echo $html;
According to your scenario
<?php
public function header() { // SOME NECESSARY PHP CODE ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="<?php //echo get_chartset ?>"/>
<link rel="stylesheet" type="text/css" href="<?php //echo_css_path ?>">
}
?>
This will echo/ print clean HTML code in front.
You could introduce tabs when you print:
print("<!DOCTYPE HTML>"
. "<html>"
. "\t<head>"
. "\t\t<meta charset=\"utf-8\"/>"
);
Another approach would be to simply just separate your html template file (Format it however you want) and then just require it with passed data in your function like so
my_view.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1><?= $heading ?></h1>
</body>
</html>
Your function
function showTemplate($view, array $data) {
extract($data);
require $view . '.php';
}
That way you can just call to output your view with data, like this
showTemplate('my_view', [ 'heading' => 'Awesome Page' ]);
This way your template and data would be more organized and pretty.
Another way, for doing this :
<?php
function myheader() {
?><!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<?php } ?>
I redefined the function name to avoid conflict, (off-topic).
I don't know how such a code should be indented ...
i have a loop like this below
foreach( $b as $entry) {
$title2 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<head>
<link rel='stylesheet' href='../../css/bootstrap.min.css'>
</head>
<body>";
$title2 .= "<div class='data'><div id=".$id."><span style='font-family: Web'>".$entry->pubDate." </span><a href='../../fetch.php?url=".$id."' title='$entry->title' >" .$title. "</a><br/><div class='content'>".$description."</div></div></div>";
$title3 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'>
<head>
<link rel='stylesheet' href='../../../css/bootstrap.min.css'>
</head>
<body><div class='container'>
<div class='row'>
<div class='col-lg-12' style='margin-top:20px;'>";
$title3 .= "<div class='list-group'><span style='font-family: Malithi Web'>".$entry->pubDate." </span>'<div><h4 class='list-group-item-heading'>'" .$title. "</h4></div><br/></div></div></div></div></div></div></div>";
if (!file_exists('File')) {
mkdir('File', 0777, true);
}
if (!file_exists('./File/'.date("Y-m-d"))) {
mkdir('./File/'.date("Y-m-d"), 0777, true);
}
if (!file_exists('./File/titles/'.date("Y-m-d"))) {
mkdir('./File/titles/'.date("Y-m-d"), 0777, true);
}
$File = './File/'.date("Y-m-d").'/'."File.html";
file_put_contents($File, $title2, FILE_APPEND | LOCK_EX);
$FileT = './File2/titles/'.date("Y-m-d").'/'."File2.html";
file_put_contents($FileT, $title3, FILE_APPEND | LOCK_EX);
}//foreach end
When i save the file what i want is to save it with html head and styles..but now it is saving for each time when the loop runs..i want it for just run once.
Thank you!
Do you want to have two dayly log files with the same data but differently formatted?
Then move your header , footer and filenaming logic outside of your loop.
<?php
foreach ($b as $entry) {
$body2 .= "<div class='data'><div id=" . $id . "><span style='font-family: Web'>" . $entry->pubDate . " </span><a href='../../fetch.php?url=" . $id . "' title='$entry->title' >" . $title . "</a><br/><div class='content'>" . $description . "</div></div></div>";
$body3 .= "<div class='list-group'><span style='font-family: Malithi Web'>" . $entry->pubDate . " </span>'<div><h4 class='list-group-item-heading'>'" . $title . "</h4></div><br/></div></div></div></div></div></div></div>";
}
if (!file_exists('File')) {
mkdir('File', 0777, true);
}
if (!file_exists('./File/' . date("Y-m-d"))) {
mkdir('./File/' . date("Y-m-d"), 0777, true);
}
if (!file_exists('./File/titles/' . date("Y-m-d"))) {
mkdir('./File/titles/' . date("Y-m-d"), 0777, true);
}
$File = './File/' . date("Y-m-d") . '/' . "File.html";
$title2 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<head>
<link rel='stylesheet' href='../../css/bootstrap.min.css'>
</head>
<body>";
$footer2 = "</body></html>";
file_put_contents($File, $title2 . $body2 . $footer2, FILE_APPEND | LOCK_EX);
$title3 = "<!doctype html>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'>
<head>
<link rel='stylesheet' href='../../../css/bootstrap.min.css'>
</head>
<body><div class='container'>
<div class='row'>
<div class='col-lg-12' style='margin-top:20px;'>";
$footer3 = "</div></div></div></body></html>";
$FileT = './File2/titles/' . date("Y-m-d") . '/' . "File2.html";
file_put_contents($FileT, $title3 . $body3 . $footer3, FILE_APPEND | LOCK_EX);
i add this define() function in my config.php file.
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
Now, i required config.php file into my index.php page like this :
require $abspath .'/config.php';
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
now in output i have this result:
?????????????????????
worked result :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
<?PHP echo _SIGN_; ?>
</body>
</html>
output result :
تواصل معنا
how do fix this for show unicode UTF-8 when i required config.php ?!
Fist where did you set the variable $abspath ?
Then you have to put <?php and ?> tags around your require statemet and in your config file around your php!
Now the example below works fine for me and is pretty much the same as yours!
config.php:
<?php
$options['0']['sign'] = "تواصل معنا"; //$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_', $options['0']['sign']);
?>
index.php:
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
And the Output is:
تواصل معنا
Did you try after establishing connection to the database execute:
set_charset ( "utf8")
I use MySQLi and make it this way:
//-> conncecting to db
$mysqli = new mysqli ( $hostname, $username, $password, $db );
if ( $mysqli -> connect_errno ) {
echo 'Failed to connect to MySQL: ' . $mysqli -> connect_error;
}
// change character set to utf8
if ( ! $mysqli -> set_charset ( "utf8") ) {
printf ( "Error loading character set utf8: %s\n", $mysqli->error );
}
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));
I making a PHP script which is powered by a POST request mainly, and it shows an HTML form which is like a command prompt, how can I simulate the text adding? I mean something like this (Mac OS X Terminal):
This is my code:
HTML:
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Terminal</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
include("co.php");
echo $prompt;
?>
</body>
</html>
PHP:
<?php
$command=$_POST['command'];
echo "<!DOCTYPE html>
<html lang=\"es\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<title>Terminal</title>
<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />
</head>";
$prompt .= "<header><p>Today is: ".date("M d")." of ".date("o")."</p><p class=\"command\"><label><form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\" autocomplete=OFF>www#".$_SERVER['SERVER_NAME'].":~$><input name=\"command\" type=\"text\" id=\"command\" size=\"40\" maxlength=\"500\" autofocus></label></form></p></header>";
$commandexp = explode(" ", $command);
if($commandexp[0] === "echo") {
$prompt .= $prompt.$commandexp[1].$prompt;
echo $prompt;
} else {
echo "command not found";
}
?>
Thanks in advance :)
Add a textbox field with no border and transparent background at the end of your command lines
and when user hits "enter", it sends the post request and refresh the list.