Jump to content

[SOLVED] Loss of variable?


noober

Recommended Posts

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.'; 

?>

Link to comment
https://forums.phpfreaks.com/topic/142238-solved-loss-of-variable/
Share on other sites

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.