Solved

PHP - Read File - Don't strip characters

Posted on 2013-02-04
4
298 Views
Last Modified: 2013-02-09
I have an EDI file with the following typical data that I'm trying to read using php:

<TRANSACTION SET PURPOSE CODE>,00, Original
<PURCHASE ORDER TYPE CODE>,NE
<PURCHASE ORDER NUMBER>,19191987
<PURCHASE ORDER DATE>,01/15/2013

No matter what type of file open or read I use, the data is read with the <> characters & all characters between the <> characters removed:

,00, Original
,NE
,19191987
,01/15/2013

How can I read the original data as is?

Thank you in advance.

AielloJ
0
Comment
Question by:AielloJ
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 38852937
It's probably being read fine by PHP but code like that will be hidden when displayed on a web page.  Anything between <> is assumed to be a tag.  You can convert it to &lt; and &gt; with htmlentities so it will display on a page.  If you look at the view source for this page, you will see that's exactly what they are doing to get it to display here.

http://www.php.net/manual/en/function.htmlentities.php
0
 
LVL 13

Author Comment

by:AielloJ
ID: 38853419
Dave,

Is the original characters what's "really" in the string.  I don't need to display it, just be able to parse out the tags so I can extract the data elements.  If the original strings are intact, then I can just do string operations on them.  The thing that made me think they were being filtered on the read ooperation, is that NetBeans is showing the variables filtered.

Best regards,

AielloJ
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38853886
I don't know about Netbeans, never used it.  Look at it in a browser.  The tags will be there in the "View Source" .
0
 
LVL 1

Expert Comment

by:Shaun_King_23
ID: 38854216
Hi AielloJ,

This is how i would do it:

xml format.

<AielloJ>
	<Transactions>
		<Transaction>
			<TRANSACTION_SET_PURPOSE_CODE>,00, Original</TRANSACTION_SET_PURPOSE_CODE>
			<PURCHASE_ORDER_TYPE_CODE>,NE</PURCHASE_ORDER_TYPE_CODE>
			<PURCHASE_ORDER_NUMBER>,19191987</PURCHASE_ORDER_NUMBER>
			<PURCHASE_ORDER_DATE>,01/15/2013</PURCHASE_ORDER_DATE>
		</Transaction>
	</Transactions>
</AielloJ>

Open in new window


php to parse the data:

<?php

$file = ('file.xml');
$contents = file_get_contents($file);

/*********************************************
create SimpleXML object from string for easier parsing 
*********************************************/
				
$XmlObj = simplexml_load_String($contents);

$xmlItems = $XmlObj->Transactions->Transaction;

if(empty($xmlItems)){
		echo "No Transactions to Process";
}else{
	echo "Processing Transactions";
	foreach($xmlItems as $xmlItem) {
		echo $xmlItem->TRANSACTION_SET_PURPOSE_CODE;
		echo $xmlItem->PURCHASE_ORDER_TYPE_CODE;
		echo $xmlItem->PURCHASE_ORDER_NUMBER;
		echo $xmlItem->PURCHASE_ORDER_DATE;
	}
}

?>

Open in new window


Hope this helps.

Many Thanks

Shaun
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.php tree directory? 5 56
Is this error or Notice in php error log? 6 32
Form Processing in PHP 11 32
Checking CSRF token within a function 36 13
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…
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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

11 Experts available now in Live!

Get 1:1 Help Now