mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-13 18:38:48 +00:00
FeedExpander: Remove tailing content in XML
- Move preprocessing code into overridable preprocessXml() - Auto-remove trailing data after root xml node
This commit is contained in:
parent
dee734d360
commit
c7b5a3f5b3
@ -22,14 +22,7 @@ abstract class FeedExpander extends BridgeAbstract
|
|||||||
if ($xmlString === '') {
|
if ($xmlString === '') {
|
||||||
throw new \Exception(sprintf('Unable to parse xml from `%s` because we got the empty string', $url), 10);
|
throw new \Exception(sprintf('Unable to parse xml from `%s` because we got the empty string', $url), 10);
|
||||||
}
|
}
|
||||||
// prepare/massage the xml to make it more acceptable
|
$xmlString = $this->prepareXml($xmlString);
|
||||||
$problematicStrings = [
|
|
||||||
' ',
|
|
||||||
'»',
|
|
||||||
'’',
|
|
||||||
];
|
|
||||||
$xmlString = str_replace($problematicStrings, '', $xmlString);
|
|
||||||
|
|
||||||
$feedParser = new FeedParser();
|
$feedParser = new FeedParser();
|
||||||
try {
|
try {
|
||||||
$this->feed = $feedParser->parseFeed($xmlString);
|
$this->feed = $feedParser->parseFeed($xmlString);
|
||||||
@ -59,6 +52,42 @@ abstract class FeedExpander extends BridgeAbstract
|
|||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare XML document to make it more acceptable by the parser
|
||||||
|
* This method can be overriden by bridges to change this behavior
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function prepareXml($xmlString)
|
||||||
|
{
|
||||||
|
// Remove problematic escape sequences
|
||||||
|
$problematicStrings = [
|
||||||
|
' ',
|
||||||
|
'»',
|
||||||
|
'’',
|
||||||
|
];
|
||||||
|
$xmlString = str_replace($problematicStrings, '', $xmlString);
|
||||||
|
|
||||||
|
// Remove extra content at the end of the document, if any.
|
||||||
|
// First retrieve tag name of root node, which is the first node following <?xml prolog,
|
||||||
|
// Then find the last matching </tag> in xml string and strip anything beyond that.
|
||||||
|
if (preg_match('/(?:<\?xml[^>]*\?>[^<]*<)([^ "\'>]+)/i', $xmlString, $matches) === 1)
|
||||||
|
{
|
||||||
|
$root_node_tag = $matches[1];
|
||||||
|
$last_closing_occurrence = strripos($xmlString, '</' . $root_node_tag);
|
||||||
|
if ($last_closing_occurrence !== false)
|
||||||
|
{
|
||||||
|
$closing_node_end = strpos($xmlString, '>', $last_closing_occurrence);
|
||||||
|
if ($closing_node_end !== false)
|
||||||
|
{
|
||||||
|
$xmlString = substr($xmlString, 0, $closing_node_end + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $xmlString;
|
||||||
|
}
|
||||||
|
|
||||||
public function getURI()
|
public function getURI()
|
||||||
{
|
{
|
||||||
return $this->feed['uri'] ?? parent::getURI();
|
return $this->feed['uri'] ?? parent::getURI();
|
||||||
|
Loading…
Reference in New Issue
Block a user