Djoris Posted May 29, 2011 Share Posted May 29, 2011 After a lot of lurking, thanks for all the great topics I now have a guestion where I could really use your help. I am parsing some xml data with the following php: <?php $data = file_get_contents("XML URL"); $actors = preg_match_all("/<actors>(.+)<\/actors>/", $data, $match); foreach ($match[1] as $actors) { echo $actors; } ?> For the following XML part: <actors> <actor id="1234">Actor #1</actor> <actor id="12345">Actor #2</actor> <actor id="123456">Actor #3</actor> </actors> This gives the ouput: Actor #1 Actor #2 Actor #3 Now I would like to separate the actor names by comma, like this: Actor #1, Actor #2, Actor #3 Does anybody know how to modify the php code for this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/ Share on other sites More sharing options...
mikesta707 Posted May 29, 2011 Share Posted May 29, 2011 use implode() for example $str = implode(", ", $match[1]); echo $str;//echos actor#1, actor #2, actor #3 hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1221943 Share on other sites More sharing options...
Djoris Posted May 29, 2011 Author Share Posted May 29, 2011 Thanks for your quick reply. Can you help me a bit further and implemented in the original code I wrote? I don't know if it is necessary to mention, but the output with 'actor #1' is variable it could be also Brad Pitt or so.. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1221966 Share on other sites More sharing options...
xyph Posted May 29, 2011 Share Posted May 29, 2011 Try before asking, please. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1221969 Share on other sites More sharing options...
Djoris Posted May 30, 2011 Author Share Posted May 30, 2011 I am sorry that I gave the impression that I didn't try, because I did. Tried several options including this one: <?php $data = file_get_contents("XML URL"); $actors = preg_match_all("/<actors>(.+)<\/actors>/", $data, $match); $str = implode(", ", $match[1]); foreach ($match[1] as $actors) { echo $str; //echos actor#1, actor #2, actor #3 } ?> I will have a another look at the implode function, but of course any reply is welcome Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222209 Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 You could also use SimpleXML like: // Load XML document $xml = simplexml_load_file('XML URL'); // Build an array of actor names like array("Actor #1", "Actor #2", ...) $actors = array(); foreach ($xml->actor as $actor) { $actors[] = (string) $actor; } // Echo with commas echo implode(", ", $actors); Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222213 Share on other sites More sharing options...
Djoris Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks for your suggestion to use simple xml. But somehow it isn't working, it is probably me being a newbie :'( or something with the external XML. Any suggestions would be more than welcome.. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222236 Share on other sites More sharing options...
silkfire Posted May 30, 2011 Share Posted May 30, 2011 What errors are you getting? SimpleXML needs to be installed on the server. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222245 Share on other sites More sharing options...
dreamwest Posted May 30, 2011 Share Posted May 30, 2011 $data = @file_get_contents("XML URL"); $n_data = new SimpleXmlElement($data, LIBXML_NOCDATA); foreach ($n_data->actor as $d) { $show[] = $d; } echo implode(", ", $show); Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222260 Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 $data = @file_get_contents("XML URL"); Please, don't suggest using the @ operator in a help thread. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222266 Share on other sites More sharing options...
dreamwest Posted May 30, 2011 Share Posted May 30, 2011 $data = @file_get_contents("XML URL"); Please, don't suggest using the @ operator in a help thread. Always use it in a live application Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222270 Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 Always use it in a live application Haha, good one! Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222276 Share on other sites More sharing options...
Djoris Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks guys ! I am using php5, so to my knowledge SimpleXMl should be installed on default. Any more suggestions..? Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222320 Share on other sites More sharing options...
Djoris Posted May 31, 2011 Author Share Posted May 31, 2011 I tried all the above codes but they didn't work. The simple xml gives no output at all and the other options give the Warning: implode() [function.implode]: Invalid arguments passed in /home/../public_html/wp-content/themes/test/single.php on line 132. I tried for example this: $data = file_get_contents("XML URL"); $actors = preg_match_all("/<actors>(.+)<\/actors>/", $data, $match); foreach ($n_data->actor as $d) { $show[] = $d; } echo implode(", ", $show); or this $data = file_get_contents("XML URL"); // Build an array of actor names like array("Actor #1", "Actor #2", ...) $actors = array(); foreach ($xml->actor as $actor) { $actors[] = (string) $actor; } // Echo with commas echo implode(", ", $actors); Any more tips would be appreciated. After that I promise I will buy a php book to avoid embarresing situations like this.. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222912 Share on other sites More sharing options...
salathe Posted May 31, 2011 Share Posted May 31, 2011 For the second code block, where is $xml created (you use it in the foreach but it isn't defined anywhere your post)? Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222919 Share on other sites More sharing options...
Djoris Posted May 31, 2011 Author Share Posted May 31, 2011 Oke I changed $xml to $data, but this doesn't seem to be the solution because I get the warning Invalid argument supplied for foreach(). How can I define the $xml in your given code? Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222930 Share on other sites More sharing options...
salathe Posted May 31, 2011 Share Posted May 31, 2011 See the code that I posted a while ago. It's under the comment that says // Load XML document. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1222995 Share on other sites More sharing options...
Djoris Posted May 31, 2011 Author Share Posted May 31, 2011 Yes I understand, but somehow simplexml is not working although I use php5 on my server.. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1223177 Share on other sites More sharing options...
xyph Posted May 31, 2011 Share Posted May 31, 2011 I am sorry that I gave the impression that I didn't try, because I did. Tried several options including this one: What about this one <?php $data = file_get_contents("XML URL"); $actors = preg_match_all("/<actors>(.+)<\/actors>/", $data, $match); $str = implode(", ", $match[1]); echo $str; //echos actor#1, actor #2, actor #3 ?> Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1223227 Share on other sites More sharing options...
silkfire Posted May 31, 2011 Share Posted May 31, 2011 Yes I understand, but somehow simplexml is not working although I use php5 on my server.. What do you mean by "not working"? If you don't provide errors it seems like you don't want this solved. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1223233 Share on other sites More sharing options...
Djoris Posted June 10, 2011 Author Share Posted June 10, 2011 What do you mean by "not working"? If you don't provide errors it seems like you don't want this solved. I sure would like this problem to be solved. When I try to parse the simple xml, I don't get any output at all, not even an error message. I spend a couple of days playing with the codes given in this thread but still don't get the comma's between the 'actors'. I tried the code: <?php $data = file_get_contents("XML URL"); $actors = preg_match_all("/<actors>(.+)<\/actors>/", $data, $match); $str = implode(", ", $match[1]); echo $str; //echos actor#1, actor #2, actor #3 ?> I think this should work but the preg match is not like it should be because the xml takes all the content between <actors></actors> and because the individual actors are also tagged it doesn't get it right: <actors> <actor id="1234">Actor #1</actor> <actor id="12345">Actor #2</actor> <actor id="123456">Actor #3</actor> </actors> I tried the preg match: $actors = preg_match_all("/<actor>(.+)<\/actor>/", $data, $match); But this doesn't give any output nor error messages. Anybody an idea how to get this right? Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1228105 Share on other sites More sharing options...
silkfire Posted June 10, 2011 Share Posted June 10, 2011 Can you PM the XML file I'll make an attempt to solve this for you. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1228129 Share on other sites More sharing options...
Djoris Posted June 10, 2011 Author Share Posted June 10, 2011 Can you PM the XML file I'll make an attempt to solve this for you. Thanks I just send you a PM. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1228137 Share on other sites More sharing options...
salathe Posted June 11, 2011 Share Posted June 11, 2011 Don't PM the XML! If you want help, please post relevant information publicly. Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1228348 Share on other sites More sharing options...
xyph Posted June 12, 2011 Share Posted June 12, 2011 <?php $str = '<actors> <actor id="1234">Actor #1</actor> <actor id="12345">Actor #2</actor> <actor id="123456">Actor #3</actor> </actors>'; preg_match_all( '/<actor(?!s)[^>]*+>([^<]++)/', $str, $matches ); echo implode( ', ', $matches[0] ); ?> Returns Actor #1, Actor #2, Actor #3 Quote Link to comment https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/#findComment-1228528 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.