Solved

PHP - Read File - Don't strip characters

Posted on 2013-02-04
4
297 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 82

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 82

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

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!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

762 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

21 Experts available now in Live!

Get 1:1 Help Now