noober Posted January 24, 2009 Share Posted January 24, 2009 Hi, I can read successfully test my xml and a text files separately, but I'm coming into a problem where the variable is possibly being removed or deleted by the time I want to plug it into the bottom of the document. As you can see in the top part of the code "$xmlname" is set to the product name specified in the xml document. In the bottom of the document where I use strpos, and attempt to see if the variable $xmlname is equal to a line in the text document, I can't get it to return true unless I statically put a word in, instead of the variable. Any ideas? <?php $xmlFileData = file_get_contents('datafeed1.xml'); //SimpleXML parser $xmlData = new SimpleXMLElement($xmlFileData); //Retrieving the product name $xmlname = $xmlData->transactions[0]->transaction[0]->transaction_details[0]->transaction_detail[0]->product_name; //Printing first product name print($xmlname); print(' - '); //Retrieving the product price $xmlprice = $xmlData->transactions[0]->transaction[0]->transaction_details[0]->transaction_detail[0]->product_price; //Printing first product price print($xmlprice); print "<br /><br />"; $lines = file('file.txt'); // Loop through array foreach ($lines as $line_num => $line) { echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n"; } $string1 = $xmlname; $file = file_get_contents('file.txt'); if(strpos($file, $string1) !== FALSE) echo 'The file contains the string'; else echo 'The string is not within the file.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/ Share on other sites More sharing options...
rhodesa Posted January 24, 2009 Share Posted January 24, 2009 $xmlname is an XML Object...you need to force it to a string...try updating this line: $string1 = (string)$xmlname; Quote Link to comment https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/#findComment-745223 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 $xmlData is the object. $xmlName should be a string. He did, after all, print it, earlier in his script. My guess is maybe you have a newline attached to the end of $xmlName. try trimming $xmlName first. Quote Link to comment https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/#findComment-745230 Share on other sites More sharing options...
rhodesa Posted January 24, 2009 Share Posted January 24, 2009 $xmlData is the object. $xmlName should be a string. He did, after all, print it, earlier in his script. My guess is maybe you have a newline attached to the end of $xmlName. try trimming $xmlName first. with SimpleXml, everything is an object. event elements as you dig down. when you print an object, it tells the object "give me the string version of it" and calls the __toString() magic method. when comparing though, it will try to compare the object to a string. just do a print_r($xmlName) to see for yourself Quote Link to comment https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/#findComment-745292 Share on other sites More sharing options...
noober Posted January 24, 2009 Author Share Posted January 24, 2009 Ok. Well, to my surprise it is apparently an object when I print_r($xmlname). I just changed the line to the first response provided by rhodesa and it works perfectly. Thanks everyone. $string1 = (string)$xmlname; Quote Link to comment https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/#findComment-745418 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 Well I learned something new about simplexml, as well. Quote Link to comment https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/#findComment-745429 Share on other sites More sharing options...
rhodesa Posted January 24, 2009 Share Posted January 24, 2009 Well I learned something new about simplexml, as well. yeah...i learned it the hard way...less fun but more memorable Quote Link to comment https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/#findComment-745436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.