I need to change texts in a XML file using PHP code. Then I created a code to:
1- get the file
2- replace the texts
3- save the file with other name.
Problem is that I am having some issues to replace some text in a xml file.
I am able to replace simples strings but I can not replace text with characters like '<'. Below the real code and files.
Original XML path: http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml
1) This code just changes the text Inmuebles to xxxxxxxx. This works fine
$xml_external_path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$xml = file_get_contents($xml_external_path);
$response = strtr($xml, array(
'Inmuebles' => 'xxxxxxxx'
));
$newXml = $response;
$newXml = simplexml_load_string( $newXml );
$newXml->asXml('/home/csainmobiliaria/www/pisos-NEW.xml');
2) Now, if I use this code to change the text <Table Name="Inmuebles"> to <xxxxxxxx> I get a ERROR 500.
$xml_external_path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$xml = file_get_contents($xml_external_path);
$response = strtr($xml, array(
'<Table Name="Inmuebles">' => '<xxxxxxxx>'
));
$newXml = $response;
$newXml = simplexml_load_string( $newXml );
$newXml->asXml('/home/csainmobiliaria/www/pisos-NEW.xml');
3) In the same way, if I use this code to remove the text Publicacion I get a ERROR 500.
$xml_external_path = 'http://www.csainmobiliaria.com/imagenes/fotos/pisos-NOK.xml';
$xml = file_get_contents($xml_external_path);
$response = strtr($xml, array(
'<Publicacion>' => ''
));
$newXml = $response;
$newXml = simplexml_load_string( $newXml );
$newXml->asXml('/home/csainmobiliaria/www/pisos-NEW.xml');
This is the final result I need to get:http://www.csainmobiliaria.com/imagenes/fotos/pisos-OK.xml

<Table Name="Inmuebles">to<xxxxxxxx>makes the closing</Table>invalid, and the closing<xxx..>non-existent. Use the parser and do this. Also when youget a ERROR 500check your error logs it will tell you what is wrong. If it doesn't look at the manual for error reporting functions. The<Publicacion>approach has the same issue. Don't use string functions on structured data (CSVs, JSON, XML, etc.), use the appropriate parsers.simplexmlis a parser. You should bring the file as it is into that, restructure it as needed, then output it. (There are other parsers as well if you don't like that one)