0
votes

I'd better start by saying I'm not a programmer. I'm modifying a shopping cart and now have a problem that's past my paygrade. The cart allows me to display the output of an rss feed in a block on a page. The problem is that much of the content of the feed is inside CDATA tags and so doesn't display properly. I need to strip out the CDATA tags from the feed.

The code that processes the feed is

    function fn_get_rss_feed($data)
    {
if (!empty($data['feed_url'])) {
    $data_key = 'rss_data_cache_' . (isset($data['block_data']['block_id']) ? $data['block_data']['block_id'] : 0);

    if (!empty($data['cache_time'])) {
        Registry::register_cache($data_key, $data['cache_time'], CACHE_LEVEL_TIME);
    }

    if (Registry::is_exist($data_key) == false) {
        $limit = !empty($data['max_item']) ? $data['max_item'] : 3;
        $rss_data = array();

        $rss = simplexml_load_string(fn_get_contents($data['feed_url']));

        if (!empty($rss)) {
            $it = 0;
            $items = array();
            foreach ($rss->channel->item as $item) {
                if ($it > $limit) {
                    break;
                }

                $items[] = array(
                    'title' => (string)$item->title,
        'description' => (string)$item->description,
                    'pubDate' => (string)$item->pubDate,
                    'link' => (string)$item->link
                );

                $it++;
            }

            $rss_data = array(array(
                $items,
                (string)$rss->channel->link,
                $data['feed_url']
            ));

            Registry::set($data_key, $rss_data);
        }

        return $rss_data;
    } else {
        return Registry::get($data_key);
    }
     }

        return array();
       }

It's the "description" that's got CDATA tags around it. Searching the net I found this code for removing CDATA tags from a string.

     function strip_cdata($string)
    {    preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
    return str_replace($matches[0], $matches[1], $string);
    }

I assume there's a way to use that function to strip the cdata tags from around the description - or maybe there's not. The output of the rss feed is displayed via a .tpl file.

Any and all help gratefully recieved.

Bruce

1

1 Answers

1
votes

Just changing the line

 $rss = simplexml_load_string(fn_get_contents($data['feed_url']));

to

 $rss = simplexml_load_string(fn_get_contents($data['feed_url']), 'SimpleXMLElement', LIBXML_NOCDATA);

should work. The third parameter instructs the underlying library used by the simplexml functions to merge the content of the CDATA into the text node (see http://php.net/manual/en/function.simplexml-load-string.php and http://www.php.net/manual/en/libxml.constants.php).