Solved

Php: ASCII to plain readable text

Posted on 2013-06-04
13
319 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 82

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 108

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
 

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 108

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
Highfive Gives IT Their Time Back

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!

 
LVL 82

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 33

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 82

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 108

Expert Comment

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now