Define entity in xml using php

I have a php script which uses simplexml to load an xml file into arrays. A variable is assigned to the array and then the data is inserted into a mysql table. It works but I am having problems when certain characters are present in the xml file. An example is ‘Atilde’ which I believe needs to be converted to ‘ã’. The code then brings up the error: ‘Entity ‘Atilde’ not defined’. There are other characters and I 'm not sure on how to achieve this.

$mpf = simplexml_load_file ("feed/videogames.xml");

foreach($mpf->merchant as $merchant)
    $merchant_name = $merchant['name'];
		foreach($merchant->prod as $product){
		$aw_thumb = $product->uri->awThumb;
		$aw_prod = $product['id'];
		$merch_prod = $product->pId;
		$brand = $product->brand->brandName;
		$pName = $product->text->name;
		$pDescription = $product->text->desc;

I have been struggling with this for a few days now and am convinced it’s something simple. Can anyone help please?

Which line is throwing the error? A mockup of your troublesome XML entry would be helpful.

apologies, the line which causes the problem is:

$mpf = simplexml_load_file ("feed/videogames.xml");

the error is ‘Warning: simplexml_load_file() [function.simplexml-load-file]: feed/videogames.xml:18045: parser error : Entity ‘Atilde’ not defined in C:\wamp\www\ est_xml\xml_feed.php on line 6’

at the moment I’m just printing out the results to get things working

Right… this seems to be because your atlide is a HTMLSPECIALCHAR…

I’m probably going to get my wrist slapped and told not to do it this way, but give this a try, it cant hurt:

$mpf = simplexml_load_string (html_entity_decode(file_get_contents("feed/videogames.xml")));

Cheers, will try it and let you know how I get on.

Whoops…get an error

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 67449756 bytes) in C:\wamp\www\ est_xml\xml_feed.php on line 8

o.o You got a reallllly big XML file my friend. That or a very nasty loop somewhere.

may be a very big file me thinks, i will test it out a bit more on some smaller xml files however. Thanks for the help.

The fact, looking back at your original error, that the first htmlentity error occured on line 18,045 of the XML file, suggests you’ve got a -REALLY- big XML file, which simpleXML (and for that matter, PHP in general) is not designed to handle at a single time.

OK, not really getting anywhere with this but need to get it working. Is there a way of checking to see if an XML element contains a certain string, if it does then it simply ignores it? All this needs to be done through php as I can’t edit the XML file (feed).

I have tried to check the string inside the variable I have created from the the array but I don’t think it’s getting that far. Used this to check where the error is occuring in the XML file:

my variable is $merchLink = $product->uri->mLink;

if (strpos($merchLink, 'Atilde') !== false)

the code runs through and bypasses the above instruction so I’m guessing it’s not putting in the array for that particular product. Shows the error but is filling in the remaining variables.

the xml line causing the problem is:

<mLink>;&#194;&#162;&#195;&#162;&#226;&#194;&#172;&#195;&#162;&#226;&#172;&#197;&lt;/mLink&gt;. Again, this is a feed so out of my control as far as editing.

you could try using preg_match… though unless i’m -entirely- mistaken… you’re not searching for Atilde, you’re searching for Ã

Tried it but doesn’t work. Just trying something else.