我从几个网站上获得了RSS feed,我想替换描述中的字符串。
说明如下:
<description><![CDATA[
<p>Some text</p>
<p>The post <a rel="nofollow" href="</a> appeared first on <a rel="nofollow" href="</a>.</p>
]]></description>
在第二<p>
中,句子为“帖子(帖子标题)首先出现在(链接)上。”
我想删除该内容,因为我已经引用了该网站,并且只想获取描述本身。
这是我的代码:
//Strip HTML Tags From The Description, As You See It Contains Some HTML Tags.
$content = strip_tags($item->description);
//Converting The 'XMLObject' Into 'String'.
$content = (string)$content;
//Checking If The Description Contains The Sentence.
if (strpos($content, 'appeared first')) {
//Array With The Links That May Exist At The End Of The Sentence.
$links = array('a', 'b', 'c');
//Loop Through These Links.
foreach ($links as $link) {
//If The Description Contains One Of The Links.
if(strpos($content, $link)){
//The Dynamic String The Would Be Searched For.
$appeared = 'The post '.$item->title.' appeared first on '.$link;
//Replace That String With Empty String.
$content = str_replace($appeared, '', $content);
echo $content;
}//End If Condition.
}//End For Each.
}//End If Condition.
因此,例如,如果句子是“ API首先出现在a上”,则条件为true,并且数组中的第一个索引存在,然后应删除该句子,并在不包含该句子的情况下打印说明。
它适用于一些,但不是全部,我也不知道为什么。
有什么我想念的吗?有没有更好的方法?