2013-04-18 18 views
1

我有一個腳本,它讀取XML標籤並將其打印在我的頁面上。 有些項目是鏈接(沒有HTML標籤),所以我做了一個向其添加HTML鏈接標籤的功能。 但是,鏈接呈現爲字符串而不是HTML。PHP:createTextNode作爲對象

我知道它是因爲我使用了createTextNode,所以我需要將它作爲對象返回而不是字符串。

繼承人我的代碼

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+)/', '\[email protected]<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(); 
} 
+0

它可以說是本來就容易用XSLT如果你會問我:) –

+0

順便說一句,你是什麼意思呈現爲字符串..不會HTML也是一個字符串? –

+0

我的意思是標記呈現爲link to page而不僅僅是一個可點擊的鏈接。 –

回答

0

可能有其他的方式,但我會通過創建兩個文本節點和主持人,你正在執行的正則表達式匹配解決這個問題:

$node = $dom->createElement('p'); 
linkify($doc, $node, 'T1 @test T2 #test T3 http://www.stackoverflow.com T4'); 

linkify()函數採用現有節點並根據給定的字符串創建文本節點和錨點。

function linkify($dom, $node, $text) 
{ 
    // we combine all possible patterns in one expression 
    $re = '/(https?:\/\/\S+)|(?:^|\s)@(\w+)|(?:^|\s)#(\w+)/'; 
    // we capture pattern offsets 
    $flags = PREG_SET_ORDER | PREG_OFFSET_CAPTURE; 

    $offset = 0; 
    if (preg_match_all($re, $text, $matches, $flags)) { 
     foreach ($matches as $match) { 
      // set start and end markers of the matched pattern 
      $start = $match[0][1]; 
      $end = $match[0][1] + strlen($match[0][0]); 
      // process each pattern based on the resulting matches 
      if (isset($match[3])) { // Twitter hash 
       $start += 2; // skip space and # 
       $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) { 
       // we have text in between previous match (or start) and here 
       $str = substr($text, $offset, $start - $offset); 
       $node->appendChild($dom->createTextNode($str)); 
      } 
      // insert the new anchor 
      $node->appendChild($anchor); 
      // keep track of last match marker 
      $offset = $end; 
     } 
     // add last text node 
     $node->appendChild($dom->createTextNode(substr($text, $offset))); 
    } 
} 

這將是輸出當您運行->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> 

我使用一個方便的功能來創建錨:

function anchor($dom, $url, $text) 
{ 
    $anchor = $dom->createElement('a', $text); 
    $anchor->setAttribute('href', $url); 
    $anchor->setAttribute('class', 'preg-links'); 

    return $anchor; 
}