Solved

Problems reading a text file with PHP

Posted on 2011-02-11
7
331 Views
Last Modified: 2013-12-13
I'm new to PHP and keep running into issues when I expect it to respond like C/C++ and it doesn't.

In this case I want to read a text file from the server and display to a web page while maintaining the formatting. Here's my code:


    if (file_exists($reqDescrFile)) {
      if ($fp = fopen($reqDescrFile, 'r')) {
        $reqDescr = fread($fp, filesize($reqDescrFile));
        fclose($fp);
        $reqDescr2 = nl2br (htmlspecialchars ($reqDescr));
        echo $reqDescr2;
      }
    }

The problem is that fread doesn't appear to read ascii chars as I'm used to. When encountering quotes (') or (") it displays a black-diamond-? to the web page. The htmlspecialchars has no affect.

Text file:  It's more than
Web page output:   It¿s more than
0
Comment
Question by:jimdgar2
  • 4
  • 3
7 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 34873229
That usually means it's not a plain ASCII character.  The Windows single and double quote pairs are not ASCII but I think part of Windows 1252 character set.  What character set is your page and PHP file using?
0
 

Author Comment

by:jimdgar2
ID: 34873255
I don't know. Is this where it's set?

    <meta charset="utf-8">

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34873359
Yes, that's for the web page but it's an incomplete declaration.  The correct line (taken from this page that you're reading) is below.  You also have to set it separately for PHP sometimes.  Here's a page about character encoding in PHP: http://webcollab.sourceforge.net/unicode.html
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jimdgar2
ID: 34873902
Nope, neither the full line nor the page on encoding help.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34873956
I would bring the text file into a text editor that can change the character set like PsPAD http://www.pspad.com/ to see what's going on.
0
 

Author Comment

by:jimdgar2
ID: 34877079
Took me quite a while to figure this out. I created the .txt file using Notepad, which saves the text file in ansi_x3.110-1983 format, which is apparently done for backward compatibility issues. By opening the file with Wordpad and then saving as a text document in MS-DOS format, the PHP code in the browser is then able to read the file correctly. Still don't quite get it but it works.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34877090
Thanks for the points.  I run into this kind of thing all the time with 'text' that people copy and paste from Microsoft Word which includes some characters only found in the standard Windows character set.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question