/    Sign up×
Community /Pin to ProfileBookmark

How to Extract Text from XML Node to Build Form Dropdowns?

I’m using SimpleXML to parse an RSS feed (from a URL) and output the content as a list via HTML. Works fine. Now I need to add two dropdowns above the listing to allow the user to narrow down what items get displayed on page refresh. The issue is the options for the dropdowns need to be extracted from HTML text in the <description> node. And I need to extract them so that I can create a unique list of dropdown options with no duplicates and are in alphabetical order.

Here’s the format for the XML:

[CODE]<item>
<title>Listing Title</title>
<link>Listing URL</link>
<description>&lt;table&gt;&lt;tr&gt;&lt;td&gt;Position&lt;/td&gt;&lt;td&gt;Text Here&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Category&lt;/td&gt;&lt;td&gt;Text Here&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Start Date&lt;/td&gt;&lt;td&gt;01/15/2015&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;City&lt;/td&gt;&lt;td&gt;Text Here&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;State&lt;/td&gt;&lt;td&gt;Text Here&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Post Date&lt;/td&gt;&lt;td&gt;01/15/2015&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</description>
<pubDate>Listing Date</pubDate>
<guid>Listing ID</guid>
</item>
<item>

</item>
[/CODE]

And here’s my current PHP/HTML:

[CODE]<?php
$joblist = simplexml_load_file(‘XML URL’);
foreach ($joblist->channel->item as $jobinfo) {
?>
<div class=”item”>
<a href=”<?php print $jobinfo->link; ?>” target=”_blank”><strong><?php print $jobinfo->title; ?></strong></a>
<?php print $jobinfo->description; ?>
<a href=”<?php print $jobinfo->link; ?>” target=”_blank” class=”read_more”>Read More</a>
</div>
<?php } ?>[/CODE]

Again — I need to create a dropdown with city options and a dropdown with category options. These values are stored in the <description> node — in HTML table cells. How do I extract those values out and build an array that is unique (no duplicates) and is in alphabetical order so that I can output form dropdowns?

Note that I don’t have the ability to change the format of the XML file. I have to work with the current format.

Any help you can give me would be greatly appreciated. Thank you.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@msmith29063authorJan 18.2015 — Nevermind. Was able to get it sorted out with some help:
[code=php]$joblist = simplexml_load_file('XML URL');
foreach ($joblist->channel->item as $jobinfo) {
$tags = array("&lt;table&gt;","&lt;tr&gt;","&lt;td&gt;","&lt;/tr&gt;","&lt;/table&gt;");
$desc = str_replace($tags, "", $jobinfo->description);
$descarr = explode("&lt;/td&gt;", $desc);
$cat[] = $descarr[3];
$city[] = $descarr[7];
}
sort($cat);
sort($city);[/code]

Then I just added an array_unique() and trim().

Thanks.
×

Success!

Help @msmith29063 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...