Jump to content

Comma's between xml content output


Djoris

Recommended Posts

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  8)

Link to comment
https://forums.phpfreaks.com/topic/237791-commas-between-xml-content-output/
Share on other sites

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 ;)

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);

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

 

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
?>

  • 2 weeks later...

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?

<?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

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.