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

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

PHP - Read File - Don't strip characters

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
AielloJ
Asked:
AielloJ
  • 2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
AielloJAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Shaun_King_23Commented:
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

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!

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