Delete some informations in a json variable with php -
my problem parsing xml file , file contain information don't want exporting json data. in case want function return json data first '[' caratere php code:
<?php class xmltojsonconverter { public function parsexml ($url) { $filecontents= file_get_contents($url); // remove tabs, newline, whitespaces in content array $filecontents = str_replace(array("\n", "\r", "\t"), '', $filecontents); $filecontents = trim(str_replace('"', "'", $filecontents)); $myxml = simplexml_load_string($filecontents); $json = json_encode($myxml); return $json; } } //path of xml file $url= 'http://www.lequipe.fr/rss/actu_rss_football.xml'; //create object of class $jsonobj = new xmltojsonconverter(); //pass xml document class function $myjson = $jsonobj->parsexml($url); print_r ($myjson); ?>
this part of json result :
{"@attributes":{"version":"2.0"},"channel":{"title":"l'equipe.fr actu football","link":"http://www.lequipe.fr","description":"l'equipe.fr, toute l'actualit\u00e9 du football","language":"fr","copyright":"copyright l'equipe.fr","pubdate":"wed, 22 apr 2015 16:31:08 +0200","image":{"url":"http://www.lequipe.fr/rss/logo_rss.gif","title":"l'equipe.fr","link":"http://www.lequipe.fr","width":"119","height":"28"},"item":[{"title":"foot - cha
i want result start '['
thank you
remove every attribute don't want before encoding json:
public function parsexml ($url) { $filecontents= file_get_contents($url); // remove tabs, newline, whitespaces in content array $filecontents = str_replace(array("\n", "\r", "\t"), '', $filecontents); $filecontents = trim(str_replace('"', "'", $filecontents)); $myxml = simplexml_load_string($filecontents); //-------------- unset($myxml['@attributes']); unset($myxml['channel']); unset($myxml['image']); //-------------- $json = json_encode($myxml); return $json; }
or if need item:
public function parsexml ($url) { $filecontents= file_get_contents($url); // remove tabs, newline, whitespaces in content array $filecontents = str_replace(array("\n", "\r", "\t"), '', $filecontents); $filecontents = trim(str_replace('"', "'", $filecontents)); $myxml = simplexml_load_string($filecontents); //-------------- $json = json_encode($myxml['item']); return $json; }
Comments
Post a Comment