Solved

Php: ASCII to plain readable text

Posted on 2013-06-04
13
332 Views
Last Modified: 2013-06-07
Hi Experts,

I'm confused. How would I convert the attached ASCII file to a plain readable string in PHP? I have tried several approaches, but can't seem to convert it correctly.

Or maybe I'm misunderstanding ASCII and my desired output...

Thanks
sample.txt
0
Comment
Question by:rmartes
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 9

Expert Comment

by:TvMpt
ID: 39220531
Hi.
Seems that there's nothing wrong with file and its correctly readable.
Maybe the format is not what you expect to see because has a lot of spaces.

Just use the trim() function. But i guess without the spaces still not making sense.
where did you get the file? Table query extraction?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39220679
I agree with @TvMpt, there are nothing but plain ASCII characters in that file.  It does have very long lines that look like they are the output of a database somewhere.  What is it you need to do with that text?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39220809
Not sure if this is what you want, but please see:
http://www.laprbass.com/RAY_temp_rmartes.php

<?php // RAY_temp_rmartes.php
error_reporting(E_ALL);
echo '<pre>';
echo PHP_EOL;

// REGULAR EXPRESSION TO REMOVE EXTRANEOUS BLANKS
$rgx
= '#'        // REGEX DELIMITER
. ' '        // BLANK CHARACTER
. '+'        // INDICATES MORE THAN ONE OF THESE CHARACTERS
. '#'        // REGEX DELIMITER AT END
;

// READ THE DOCUMENT
$str = file_get_contents('http://filedb.experts-exchange.com/incoming/2013/06_w23/658258/sample.txt');

// REMOVE EXTRANEOUS BLANKS, REPLACING WITH ONE BLANK
$new = preg_replace($rgx, ' ', $str);

// SHOW THE WORK PRODUCT
print_r($new);

Open in new window

If that's not it, please post back with an explanation of what you really need, thanks.  HTH, ~Ray
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rmartes
ID: 39220811
Thanks guys,

The file is actually an ACS (Address Change Service) notification file from USPS. We get these files daily. They don't mention how to read or convert/decode the file to readable text or what each section even means. They only mention how to download the ASCII files using their web service. I got the web service working, but don't know where to go from there as far as decoding.

I have attached a sample of their documentation. I've been at it for 3 days now, and can't seem to get it.
Screen-Shot-2013-06-04-at-8.42.0.png
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39220816
When you read this file, do you find HTML or XML markup?  If you read it with a browser, please use "view source" and show us any markup associated with these text fields.  Thanks, ~Ray
0
 

Author Comment

by:rmartes
ID: 39220904
View source gives me the same thing you see in the sample attached above. No markup language. I will work with your php script above in cleaning the string and will get back to you. I am coming to the conclusion that there is nothing wrong with the file contents. USPS is intentionally sending these files in this format.

If you find software that can decode or format this file, pls let know. Otherwise, I will test your code out above and get back to you.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39221066
That is a formatted name and address file.  You said ACS (Address Change Service) which probably means you are doing bulk mailing thru the Post Office and your mailing list is supposed to be updated when customer addresses change.  That's the info to do it.  Somewhere is the information to tell you what each column or position is supposed to mean.  Some like the name is fairly obvious and some of the other fields are obviously part of the address.
0
 
LVL 9

Accepted Solution

by:
TvMpt earned 500 total points
ID: 39221412
Hi.
Theres your answer

You have to know the headers from file to extract data. Something like this:

http://about.usps.com/publications/pub8a/8atext_021.html

then you can use substr or somthing to map the fields to an object that make sense to you.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 39223681
greetings  rmartes, , I looked at your sample.txt file in a HEX editor, and it is Normal ENGLISH ASCII TEXT format, for windows-dos Line-breaks, you in NO-Way need to convert this text, it is already  "Normal ENGLISH ASCII TEXT format"

However there is ONE NON "english keboard ASCII TEXT", character at the very end as  binary number 26 ( 1A hex ), referred to in ASCII as "substitute" character. I would think this is their "End Of File" character marker, so if this is the "format" of your text file all you need to do is remove the LAST end of file character with -

$text1 = getTEXT();// however you get this FILE text
$text1 = substr($text1, 0, -1);  // removes the last character
// the  $text1 is now ready to be used in PHP as  "Normal ENGLISH ASCII TEXT format"

you will also need to extract the data segments from each line like  -
200000002       160105          201111B 250SUFFOLK SURGICAL ASSOC PC

and it uses some sort of space character (multiple spaces) as a delimiter, but I could NOT find any pattern to the number of space characters used for a delimiter, possibly there are a certain number of space characters to signify what a data segment would be for.

I believe you are misunderstanding what the references are for "ASCII file" and a "plain readable string", as far as I know they are the same, "no difference" as readable English text must be ASCII.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39223749
binary number 26 ( 1A hex ) is Ctrl-Z which has been used as End-of-File since before I started using computers in 1979.  The files are fixed position data files.  They can be imported in Excel by identifying the column positions when you import it.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39224401
@Dave: 1979?!  You're quite the youngster ;-)  Cheers, ~Ray
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39224462
Actually I think it 1978 when I borrowed one.  I got my own in 1979.
0
 

Author Closing Comment

by:rmartes
ID: 39230551
Exactly what I was looking for!
0

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.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 …

685 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