Jump to content

Comma's between xml content output


Djoris
 Share

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
Share on other sites

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  :D

Link to comment
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 ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
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:

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

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