I have a script that reads XML tags and prints it on my page. Some items are links (without HTML tags) so I made a function that adds HTML link tags to it. However, the links are rendered as strings and not as HTML.
I know its because I used createTextNode, So i need something that returns it as an object and not as a string.
Heres my code
function get_feeds() {
$feeds = array(
array(
'name' => 'Agenda',
'url' => 'http://www.beleefdokkum.nl//pages/rss.aspx?type=agenda',
'get' => array('title', 'description', 'link'),
'scope' => array(1, 10)
),
array(
'name' => 'News',
'url' => 'http://www.beleefdokkum.nl//pages/rss.aspx?type=nieuws',
'get' => array('title', 'description', 'link'),
'scope' => array(1, 10)
),
array(
'name' => 'Social media',
'url' => 'http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=NOFriesland',
'get' => array('description'),
'scope' => array(1, 10)
)
);
$result = new DOMDocument();
function linkify($text) {
$text = preg_replace('/(https?:\/\/\S+)/', '<a href="\1" class="preg-links">\1</a>', $text);
$text = preg_replace('/(^|\s)@(\w+)/', '\1@<a href="http://twitter.com/\2" class="preg-links">\2</a>', $text);
$text = preg_replace('/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2" class="preg-links">\2</a>', $text);
return $text;
}
foreach ($feeds as $feed) {
$xml = new DOMDocument();
$xml->load($feed['url']);
$frame = $result->createElement('div');
$frame->setAttribute('class', 'feed_frame');
$result->appendChild($frame);
$name = $result->createElement('h1', $feed['name']);
$name->setAttribute('class', 'feed_name');
$frame->appendChild($name);
$content = $result->createElement('div');
$content->setAttribute('class', 'feed_content');
$frame->appendChild($content);
for ($i = $feed['scope'][0]; $i < $feed['scope'][1]; $i++) {
$item = $result->createElement('span');
$item->setAttribute('class', 'feed_item');
$content->appendChild($item);
foreach ($feed['get'] as $get) {
$object = $result->createElement('p');
$text = $result->createTextNode(linkify($xml->getElementsByTagName($get)->item($i)->nodeValue));
$object->appendChild($text);
$object->setAttribute('class', 'feed_'.$get);
$item->appendChild($object);
}
}
}
return $result->saveHTML();
}