Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

Php: ASCII to plain readable text

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
rmartes
Asked:
rmartes
  • 4
  • 3
  • 3
  • +2
1 Solution
 
TvMptCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
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
Independent Software Vendors: 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!

 
rmartesAuthor Commented:
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
 
Ray PaseurCommented:
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
 
rmartesAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
TvMptCommented:
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
 
Slick812Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
@Dave: 1979?!  You're quite the youngster ;-)  Cheers, ~Ray
0
 
Dave BaldwinFixer of ProblemsCommented:
Actually I think it 1978 when I borrowed one.  I got my own in 1979.
0
 
rmartesAuthor Commented:
Exactly what I was looking for!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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