Replace links in a xml or rss file

Discussion in 'Programming' started by webuk44, May 26, 2007.

  1. #1
    I want to change the output of my blog feed.
    At the moment all posts have links within <link></link> tags.
    How can I swap ALL links with my link "mylink.com"
    Kind of search/replace but need to replace ANY links that occur within these tags.
    File would be a xml file.
    Resulting file would still be a xml file.
    Either javascript or PHP?
     
    webuk44, May 26, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?
    function replace_links_in_xml_with( $url, $xml )
    {
    	return 	preg_replace('~<link>(.*?[^<])</link>~si', 
    			sprintf( '<link>%s</link>', $url ), 
    			file_get_contents( $xml ) 
    	);
    }
    echo replace_links_in_xml_with( 'http://forums.digitalpoint.com', 'http://krakjoe.com/rss/projects' );
    ?>
    
    PHP:
     
    krakjoe, May 26, 2007 IP