• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Problems reading a text file with PHP

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
jimdgar2
Asked:
jimdgar2
  • 4
  • 3
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
jimdgar2Author Commented:
I don't know. Is this where it's set?

    <meta charset="utf-8">

0
 
Dave BaldwinFixer of ProblemsCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jimdgar2Author Commented:
Nope, neither the full line nor the page on encoding help.
0
 
Dave BaldwinFixer of ProblemsCommented:
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
 
jimdgar2Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now