If an item is being processed with the Node processor and has no title, the processor will take the first 3 words of the body and use them as the node title.

But if the body starts with an image, the node title ends up being html markup gibberish.

The magic happen in the feedapi_node.module file on line 287 (1.3)

  $node->title = $feed_item->title;
  if (empty($node->title) && !empty($feed_item->description)) {
    // Explode to words and use the first 3 words.
    $words = preg_split("/[\s,]+/", $feed_item->description);
    $node->title = $words[0] .' '. $words[1] .' '. $words[2];
  }

An easy solution would be to add a strip_tags function to $feed_item->description

It would look like this

  $node->title = $feed_item->title;
  if (empty($node->title) && !empty($feed_item->description)) {
    // Explode to words and use the first 3 words.
    $words = preg_split("/[\s,]+/", strip_tags($feed_item->description));
    $node->title = $words[0] .' '. $words[1] .' '. $words[2];
  }

Comments

zis’s picture

Version: 6.x-1.3 » 6.x-1.x-dev

After 2 days of testing it seems to be working fine.

Anyone else can confirm?

Antinoo’s picture

I agree to submit your piece of code.

I noticed nodes from a feed were taken with an ugly title, made of html tags.
Initially, I thought the feed was giving me an html title.
Then, I realized it was giving me no title at all.

This is the code I'm running at the moment:

  $node->title = filter_xss($feed_item->title, array()); // filter_xss() added
  if (empty($node->title) && !empty($feed_item->description)) {
    // Explode to words and use the first 3 words.
    $words = preg_split("/[\s,]+/", filter_xss($feed_item->description, array())); // filter_xss() added
    $node->title = $words[0] .' '. $words[1] .' '. $words[2];
  }

It's quite the same of your one.
I've used filter_xss with an empty array as 2nd arg. But strip_tags should be a better choice, I think.

Ciao, Giovanni

Antinoo’s picture

Title: Node title if item has no title and body starts with image » Strip tags from node title

better title, I was missing this issue

zis’s picture

  $node->title = filter_xss($feed_item->title, array()); // filter_xss() added
  if (empty($node->title) && !empty($feed_item->description)) {
    // Explode to words and use the first 3 words.
    $words = preg_split("/[\s,]+/", strip_tags($feed_item->description)); // strip_tags() added
    $node->title = $words[0] .' '. $words[1] .' '. $words[2];
  }

Strip tags is more efficient, filter_xss is overkill. But a filter_xss should be added in the first instance (if a title already exists)

Antinoo’s picture

I think filter_xss($string, array()) and strip_tags($string) would return the same string.
Hence, I can't see why we should use filter_xss() for the title, if strip_tags() is more efficient. ;)

It would be useful to have an option to change the 2nd argument passed to filter_xss(), which lists the allowed tags (I want no tags in the title, that's why I used an empty array).

Anyway, I think all the filtering code should be put in the hook_filter().

aron novak’s picture

Status: Needs review » Fixed

A different method (but strip tags) is committed into both parsers. (see the parser files, this functionality is moved!)

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.