Solved

Problems reading a text file with PHP

Posted on 2011-02-11
7
327 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 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
I don't know. Is this where it's set?

    <meta charset="utf-8">

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jimdgar2
Comment Utility
Nope, neither the full line nor the page on encoding help.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now