SimplePie RSS解析 - 从get_title中随机取出单词?

时间:2022-07-02 00:57:49

I'm currently using SimplePie to parse RSS news feeds. I am successfully merging the feeds using an array, however what I need to do is return single words at random from the title, not just the whole title. Can this be done in PHP at all? I was toying around with explode(); however didn't have any luck.

我目前正在使用SimplePie来解析RSS新闻源。我使用数组成功合并了feed,但我需要做的是从标题中随机返回单个单词,而不仅仅是整个标题。这可以在PHP中完成吗?我正在玩弄爆炸();但是没有任何运气。

Do I need to introduce some Javascript of some sort, after the data is parsed? I know this is a little vague, I'm just trying to get a sense of what is possible (I am open to using an alternative to SimplePie, this is just what I have used so far).

在解析数据后,是否需要引入某种类型的Javascript?我知道这有点模糊,我只是想了解什么是可能的(我愿意使用SimplePie的替代品,这就是我到目前为止所使用的)。

Here is my code right now, which simply returns the titles as whole:

这是我现在的代码,它只返回整个标题:

<?php
//link simplepie
require_once ('simplepie/autoloader.php');

//new simplepie class
$feed = new SimplePie();

$feed->enable_cache(true);

$feed->set_cache_duration(60);

//set up feeds
$feed->set_feed_url(array('http://mf.feeds.reuters.com/reuters/UKTopNews' , 'http://www.theguardian.com/world/rss'
));

//run simplepie
$feed->init();

//handle content type
$feed->handle_content_type();

?>

<!DOCTYPE html>
<head>

<title>News</title>

<link rel="stylesheet" type="text/css" href='style.css'>

</head>

<body>

<div class = "headlines">

<?php foreach ($feed->get_items(0, 10) as $item): ?> 

<?php $item->get_title(); ?>

<h4><?php echo $item->get_title(); ?></h4>

<?php endforeach; ?>

</div>

</body>

</html>

Thanks!

1 个解决方案

#1


0  

I need to do is return single words at random from the title

我需要做的是从标题中随机返回单个单词

I hope i got your question right, "return a random word from title", right? Your problem is unrelated to SimplePie. Whenever you have a problem, try to reduce it to the minimum problem: here it's only a "how to work with strings" problem.

我希望我的问题是正确的,“从标题中随机回答”,对吧?您的问题与SimplePie无关。每当遇到问题时,请尝试将其减少到最小的问题:这里只是“如何使用字符串”问题。

For your use-case:

对于您的用例:

$title = $item->get_title();
echo array_rand(array_flip(explode(' ', $title)), 1);

Standalone example:

$string = 'This is an example headline and it contains a lot of words.';
echo array_rand(array_flip(explode(' ', $string)), 1);

How does this work:

这个怎么用:

First the title string is exploded at the space char. You get an array back. It's key=>value, where value is a word from the string. Now, we flip values and keys - to get the values as keys and then we random pick 1 element by using array_rand().

首先,标题字符串在空格char处展开。你得到一个阵列。它是key => value,其中value是字符串中的单词。现在,我们翻转值和键 - 将值作为键,然后我们使用array_rand()随机选择1个元素。

This might needs some additional tweaking to cut commas and fullstops away and get it working with special chars. But it should get you started.

这可能需要一些额外的调整来删除逗号和fullstops并使其与特殊字符一起使用。但它应该让你开始。

#1


0  

I need to do is return single words at random from the title

我需要做的是从标题中随机返回单个单词

I hope i got your question right, "return a random word from title", right? Your problem is unrelated to SimplePie. Whenever you have a problem, try to reduce it to the minimum problem: here it's only a "how to work with strings" problem.

我希望我的问题是正确的,“从标题中随机回答”,对吧?您的问题与SimplePie无关。每当遇到问题时,请尝试将其减少到最小的问题:这里只是“如何使用字符串”问题。

For your use-case:

对于您的用例:

$title = $item->get_title();
echo array_rand(array_flip(explode(' ', $title)), 1);

Standalone example:

$string = 'This is an example headline and it contains a lot of words.';
echo array_rand(array_flip(explode(' ', $string)), 1);

How does this work:

这个怎么用:

First the title string is exploded at the space char. You get an array back. It's key=>value, where value is a word from the string. Now, we flip values and keys - to get the values as keys and then we random pick 1 element by using array_rand().

首先,标题字符串在空格char处展开。你得到一个阵列。它是key => value,其中value是字符串中的单词。现在,我们翻转值和键 - 将值作为键,然后我们使用array_rand()随机选择1个元素。

This might needs some additional tweaking to cut commas and fullstops away and get it working with special chars. But it should get you started.

这可能需要一些额外的调整来删除逗号和fullstops并使其与特殊字符一起使用。但它应该让你开始。