Search code examples
phpxmlparsingreplacefeed

Remove contextual links inside a description tag of a feed without losing the value


I would like to know if it's possible to remove all contextual links inside a <description> tag of an XML feed.

Here is what I have got inside the PHP file:

<?php
    header('Content-type: text/xml');
    $url = "http://www.Example.com/articles.rss";
    $page = file($url);

    foreach ($page as $part)
    {
        echo $part;
    }
?>

See screenshot of the XML feed:

https://i.sstatic.net/VTsBf.jpg


Solution

  • Use preg_replace to find links and replace them with empty lines.

    Match opening <a> tag and then closing </a>:

    foreach ($page as $part)
    {
      $part = preg_replace("/<a\b[^>]*>/", "", $part);
      $part = preg_replace("/<\/a>/", "", $part);
      echo $part;
    }
    

    Live demo: http://www.rubular.com/r/9dWWxbhzLn