Adrienne
Adrienne

Reputation: 2680

How to remove HTML tags from RSS feed?

I am using Google Feed API to extract blog entries from a tumblr feed.

I have been able to pull the content, but the output comes out with the html tags as such:

<p>I remember one day asking one of my mentors James if he ever got nervous around people. James replied, “Only when I need something from them.”</p>

The code is simple, as below:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <script type="text/javascript">
google.load("feeds", "1");

 function initialize() {
   var feed = new google.feeds.Feed("http://adriennetran.tumblr.com/rss");
   feed.load(function(result) {

     if (!result.error) {
       var container = document.getElementById("feed");

       for (var i = 0; i < result.feed.entries.length; i++) {
         var entry = result.feed.entries[i];
         window.content = document.createTextNode(entry.content);
         container.appendChild(content);
       }
     }
   });
 }


 google.setOnLoadCallback(initialize);

 </script>

I tried writing a function to strip everything that starts with < as such:

content_array = content.split(" ");

for (i=0; i < content_array.length; i++){
    if ((content_array[i].split(""))[0] == "<"){
      content_array.splice(i, 1);
    }
}

content2 = content_array.toString();

But I got an Uncaught TypeError: undefined is not a function error because content is an object and not a string and therefore I cannot call content.split(" ").

I've tried converting to a string, but this is the output from console

typeof(content)
> "object"

c2 = content.toString()
> "[object Text]"

Does anybody have any ideas on how to manipulate elements retrieved from RSS?

Upvotes: 2

Views: 3518

Answers (2)

Mouser
Mouser

Reputation: 13304

Let's see

var regExString = /(<([^>]+)>)/ig; //create reg ex and let it loop (g)
contentString = content.textContent // get text from node (no longer an object but string.

contentString = contentString.replace(regExString, "") //find all tags and delete them.

Upvotes: 5

BrockLee
BrockLee

Reputation: 981

If you have jQuery included on your page, you can create a node using the HTML received from your feed and get the text from the HTML like so:

var html = '<p>I remember one day asking one of my mentors James if he ever got nervous around people. James replied, “Only when I need something from them.”</p>';
var text = $(html).text(); // This gets the text from any HTML code and leaves out the tags

Upvotes: 0

Related Questions