Can't read arabic txt file in PHP - php

I have so many text(.txt) files and I want to read it with PHP and display in web browser. Some of my files is in arabic language.
I am using file_get_contents function to read files. But I can't get proper result.
Here is sample of what I is my input and output.
Input Text ===> لة إلى الشعب الأردني العزيز والى شعوب العالم الحر والى المنظمات الدولية للحرية وحقوق الإنسان والى معاقل الديم
Output Text ===> J2 H'DI 49H( 'D9'DE 'D-1 H'DI 'DEF8E'* 'D/HDJ) DD-1J) H-BHB 'D%F3'F H'DI E9'BD 'D/JEHB
My page is has already UTF-8 charset. I have also tried fopen function and still same result.
What I am missing?

This works for me:
<html>
<head>
<!-- <link rel="stylesheet" href="css/style.css" /> -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<?php
$file = "arabic.txt";
$data = file_get_contents($file);
echo $data; ?>
</body>
</html>
, where arabic.txt is saved with UTF-8 encoding.

you can try this:
<html>
<meta http-equiv='Content-Type' content='text/html'; charset='UTF-8'/>
<body>
<?php
//put your file in this folder
$path='D:\test';
$files=scandir($path);
foreach ($files as $key => $value) {
if($value!="." && $value!="..")
{
print_r(file_get_contents($path."/".$value));
}
}
?>
</body>
<html>
you can see : How to print all the txt files inside a folder with php?

Related

Accessing properties in an include when setting that include in to a file

I am trying to create a template html page which I will call via an include to set to a variable, this variable will then be used to set the value of a new file. I need the variables in the included file to be resolved so that the values are populated correctly.
To demo imagine these files:
main.php
$someVar = "someValue";
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
if (file_put_contents($newFileName, $fileText) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "not created";
}
aTemplate.php
<?php
return
'<!doctype html>
<html lang="en">
<head>
<title><?php echo $someVar; ?></title>
</head>
</html>'
?>
What is currently happening is that the variables stay unresolved and hold no value so in the created html file the title is:
<title></title>
Instead of
<title>someValue</title>
How can I change the 'aTemplate.php' file to resolve the properties set in 'main.php'?
Just use this at your aTemplate.php:
<?php
return '<!doctype html>
<html lang="en">
<head>
<title>'. $someVar .'</title>
</head>
</html>';
?>
There are a couple of problems with your template, firstly as you have the HTML in single quotes, this won't do any of the string substitutions. Secondly, your trying to do a PHP echo whilst in HTML in PHP. I've used Heredoc to enclose the HTML as it allows any sorts of quotes and will also do the replacements.
The substitution of the value is just replaced by adding $someVar directly into the string.
So aTemplate.php becomes...
<?php
return <<< HTML
<!doctype html>
<html lang="en">
<head>
<title>$someVar</title>
</head>
</html>
HTML;
You should echo those string in page instead of return command.
The keyword return is used inside a function while your file is not a function. The browser simply puts what's inside include file has to offer. In you case it is HTML string which should be outputted using echo command.
Also the server executes code in top to bottom and left to right. Thus the variable $someVar will be accessed in aTemplate.php file.
Use below code instead to work
main.php
$someVar = "someValue";
$file = 'aTemplate.php';
// Open the file to get existing content
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
// Write the contents back to the new file
if (file_put_contents($newFileName, $fileText) !== false)
{
echo "File created (" . basename($newFileName) . ")"; }
else {
echo "not created";
}
aTemplate.php
<!doctype html> <html lang="en"><head>
<title><?php echo $someVar;?></title>
</head>
</html>

How use a php variable declared in the parent file in an included file

I'm trying to pass the php variable $shareURL = "someURL"; from the parent page of test.php into the included file of commentTest.php. Is this possible? If yes, please help.
Parent File = Test.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<?php
$shareURL = "someURL";
echo "$shareURL";
include "http://domainName.net/assets/includes/commentTest.php";
?>
</body>
</html>
PHP Included File = commentTest.php
<?PHP
echo "<div class='fb-comments' data-href='$shareURL' data-num-posts='5' data-width='100%'></div>";
?>
HTML Output Source
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
someURL<div class='fb-comments' data-href='' data-num-posts='5' data-width='100%'></div></body>
</html>
Change your Test.php to this:
include "/assets/includes/commentTest.php";
Thanks everyone!
Your comments and answers helped me find the solution I needed.
$root = dirname(__FILE__);
include "$root/assets/includes/commentTest.php";
Apparently my root is here /var/www/html instead of right after the TLD in the URL.

How can I create DOM object from a HTML file in php language?

I want to parsing data from homepage on this url. As you can see this url is HTML file and I read below:
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
so I just type a code below
include "simple_html_dom.php";
$html = file_get_html('eecs.kookmin.ac.kr/site/computer/notice.htm');
echo $html->plaintext;
The error message is:
Error message Warning: file_get_contents(eecs.kookmin.ac.kr/site/computer/notice.ht‌​m): failed to open stream: No such file or directory in C:\Bitnami\wampstack-5.6.27-0\apache2\htdocs\simple_html_dom‌​.php on line 76
what should I do?
You can get the HTML code using the Snoopy Class (https://sourceforge.net/projects/snoopy). Next code displays the HTML code inside of a <textarea> tag, then it displays the page itself, copy-paste next code in a PHP file and open it in your browser:
<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=euc-kr">
<META HTTP-EQUIV="Content-language" CONTENT="ko">
</head>
<body>
<?php
require("Snoopy.class.php"); // ◄■■ GET SNOOPY FROM https://sourceforge.net/projects/snoopy
$snoopy = new Snoopy;
$snoopy->fetch("http://eecs.kookmin.ac.kr/site/computer/notice.htm");
$html = mb_convert_encoding( $snoopy->results, "UTF-8", "EUC-KR" ); // ◄■■ GET HTML CODE.
echo "<textarea rows='25' cols='80'>$html</textarea>"; // ◄■■ DISPLAY THE HTML.
echo $html; // ◄■■ DISPLAY THE WEBPAGE.
?>
</body>
</html>
The Snoopy Class is only one file, make sure the file is in the same directory your PHP file is.

Reading and encoding html

I am trying to read and display the content of the title (contained in a h1 tag) from many HTML files. These files are all in the same folder.
This is what the html files look like :
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>
<html>
<head>
<title>A title</title>
<style type='text/css'>
... Styles here ...
</style>
</head>
<body>
<h1>Être aidant</h1>
<p>En général, les aidants doivent équilibrer...</p>
... more tags ...
</body>
I have tried to display the content from the H1 tag with this PHP script :
<?php
foreach (glob("test/*.html") as $file) {
$file_handle = fopen($file, "r");
$doc = new DOMDocument();
$doc->loadHTMLfile($file);
$title = $doc->getElementsByTagName('h1');
if ( $title && 0<$title->length ) {
$title = $title->item(0);
$content = $doc->savehtml($title);
echo $content;
}
fclose($file_handle);
}
?>
But the output contains wrong characters. For the example file, the output is :
Être aidant
How can I achieve this output?
Être aidant
You should state a charset in the <head> of your HTML document.
<meta charset="utf-8">
you need to use utf-8 encoding
change echo $content to echo utf8_encode($content);

PHP redirect isn't working for some reason

I'm trying to redirect this PHP page to another page as soon as I get successful update in the database .. but I get a warning from PHP and nothing happens .. Below is the code .. Where did I go wrong ?
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="css/madscore.css">
</head>
<body>
<?php
require('../database/connect.php');
?>
<?php
$id = $_POST["id"];
$value = $_POST["score"];
database_connect();
$query = "update people set Score= Score +".$value." WHERE ID ='".$id."'";
$result = $connection->query($query);
if($result)
{
?>
<?php
#header("Location: http://www.europe-zone.com/");
exit();
}
?>
</body>
</html>
You should send headers before your HTML page. Put your redirect code just before
<html> <head>
Regarding to this:
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
<?php
require('../database/connect.php');
$id = $_POST["id"];
$value = $_POST["score"];
database_connect();
$query = "update people set Score= Score +".$value." WHERE ID ='".$id."'";
$result = $connection->query($query);
if($result)
{
?>
<?php
#header("Location: http://www.europe-zone.com/");
exit();
}
?>
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="css/madscore.css">
</head>
<body>
</body>
</html>

Categories