Solved

Problems reading a text file with PHP

Posted on 2011-02-11
7
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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