Forum Moderators: coopster

Message Too Old, No Replies

PHP Parsing XML

how do i parse colon in xml into <br> or seperate variables

         

Davco

10:41 am on Apr 16, 2005 (gmt 0)

10+ Year Member



I'm currently trying to parse and display this xml feed.

Right now, I can open the feed and rip out all the content to variables and display in a nicely formatted fashion using:

// Set up $nations
$nationsOpen = "<NATIONS>";
$nationsClose = "</NATIONS>";
$start = strpos($text,$nationsOpen);
$end = strpos($text,$nationsClose);
$length = $end - $start;

$nations = substr($text,$start,$length);

however, the <NATIONS> section contains colons. So my question is this: how can I parse the feed so that the colons become <br> tags, or the seperate nations are grabbed to individual variables?

Thanks ^_^

[edited by: coopster at 12:05 pm (utc) on April 16, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

KingMacro

5:26 pm on Apr 16, 2005 (gmt 0)

10+ Year Member



not sure exactly what you mean, but for simply replacing ; with <br> you can use str_replace

however for parsing XML you might want to look at [php.net...]

Davco

5:13 pm on Apr 17, 2005 (gmt 0)

10+ Year Member



Sorry, maybe it'll make more sense if I put the link to the feed :O

<Sorry, no urls please. -- coopster>

What Im trying to is parse the xml and display a page containing the information, however, I would like the <NATIONS> tag to be displayed as seperate strings instead of one large one. The colons are where the strings should start and end.

Im thinking the algorithm will be something like this:
A for loop, which will run the number of times found from <NUMNATIONS>. In this loop, the code will check for the next colon, and create a new variable containing the string between the two current colons. Then, in the body section of the page, another loop will print the strings to the page.

I havent been working with PHP for very long, so I might be missing something quite basic here.

Thanks in advance for any help ^_^

[edited by: coopster at 10:06 pm (utc) on April 17, 2005]
[edit reason] removed url per TOS [/edit]

Norky

5:20 pm on Apr 17, 2005 (gmt 0)

10+ Year Member



str_replace(":", "<br>", $nations);

Assuming $nations contains "pifflofodofia:the_pie_eaters:sooty_and_sweep:sevenfaust:fluffly_bunnies:bunnygirl"

...or if you want each nation in an array...


explode(":", $nations);

Davco

6:31 pm on Apr 17, 2005 (gmt 0)

10+ Year Member



im having some trouble using the explode() function.

What is the name of the array where it places my strings, and how can I call values out of the array?

using var_dump(explode(":", $nations)); I get this:

array(6) { [0]=> string(22) "pifflofodofia" [1]=> string(14) "the_pie_eaters" [2]=> string(15) "sooty_and_sweep" [3]=> string(10) "sevenfaust" [4]=> string(15) "fluffly_bunnies" [5]=> string(9) "bunnygirl" }

Which shows that the strings are stored in the array, but echo array(2), array[2], string(2) or string[2] all apear to do the wrong thing.

realitybytes

9:03 pm on Apr 17, 2005 (gmt 0)

10+ Year Member



Create a new variable ie

$my_array = explode(":", $nations);

Then you can either use further variables e.g.

$var_0 = $my_array[0];
or you can slimply use the $my_array[1];

so on and so on

Davco

9:25 pm on Apr 17, 2005 (gmt 0)

10+ Year Member



Ah thank you, now everything works perfectly ^_^

incrediBILL

10:44 pm on Apr 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Save yourself a ton of time and get some PHP XML Parser Functions

[us2.php.net...]

Norky

5:19 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Yes, but he still has to deal with extracting the individual nations. That's like parsing CSV data with an XML parser.

Davco

12:50 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



Hello again, I have encountered another problem.
This time, when I try to print a string, containing a url, into an element such as <img> or <a href=""> the XML tag is still attached to the start, so the output appears like this:
When printing to an <img> tag
[me.host.net...]

when printing to an <a> tag:
[me.host.net...]

(<FLAG> and <NATIONS> being the XML tags)

How can I solve this problem?

[edited by: jatar_k at 4:57 pm (utc) on April 19, 2005]
[edit reason] generalized urls [/edit]

incrediBILL

7:36 am on Apr 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



just use str_replace() to replace <NATIONS> and <FLAGS> with "" null.