xml - PHP: createTextNode as object -
i have script reads xml tags , prints on page. items links (without html tags) made function adds html link tags it. however, links rendered strings , not html.
i know because used createtextnode, need returns object , not string.
heres 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 $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); ($i = $feed['scope'][0]; $i < $feed['scope'][1]; $i++) { $item = $result->createelement('span'); $item->setattribute('class', 'feed_item'); $content->appendchild($item); foreach ($feed['get'] $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(); }
there might other ways, solve creating both text nodes , anchors you're performing regular expression matching:
$node = $dom->createelement('p'); linkify($doc, $node, 't1 @test t2 #test t3 http://www.stackoverflow.com t4');
the linkify()
function takes existing node , creates text nodes , anchors based on given string.
function linkify($dom, $node, $text) { // combine possible patterns in 1 expression $re = '/(https?:\/\/\s+)|(?:^|\s)@(\w+)|(?:^|\s)#(\w+)/'; // capture pattern offsets $flags = preg_set_order | preg_offset_capture; $offset = 0; if (preg_match_all($re, $text, $matches, $flags)) { foreach ($matches $match) { // set start , end markers of matched pattern $start = $match[0][1]; $end = $match[0][1] + strlen($match[0][0]); // process each pattern based on resulting matches if (isset($match[3])) { // twitter hash $start += 2; // skip space , # $url = 'http://search.twitter.com/search?q=' . urlencode("#{$match[3][0]}"); $anchor = anchor($dom, $url, $match[3][0]); } elseif (isset($match[2])) { // twitter user $start += 2; $url = 'http://twitter.com/' . urlencode($match[2][0]); $anchor = anchor($dom, $url, $match[2][0]); } else { // standard url $anchor = anchor($dom, $match[1][0], $match[1][0]); } if ($start > $offset) { // have text in between previous match (or start) , here $str = substr($text, $offset, $start - $offset); $node->appendchild($dom->createtextnode($str)); } // insert new anchor $node->appendchild($anchor); // keep track of last match marker $offset = $end; } // add last text node $node->appendchild($dom->createtextnode(substr($text, $offset))); } }
this output when run ->savehtml()
:
<p> t1 @<a href="http://twitter.com/datibbaw" class="preg-links">datibbaw</a> t2 #<a href="http://search.twitter.com/search?q=%23test" class="preg-links">test</a> t3 <a href="http://www.stackoverflow.com" class="preg-links">http://www.stackoverflow.com</a> t4</p>
i'm using convenience function create anchors:
function anchor($dom, $url, $text) { $anchor = $dom->createelement('a', $text); $anchor->setattribute('href', $url); $anchor->setattribute('class', 'preg-links'); return $anchor; }
Comments
Post a Comment