Forum Moderators: open
Here is one for you I hope you can help. Here is my code.
<?php
$a = 0;
$dom = new DOMDocument();
$dom->load( 'http://www.example.co.uk/rss.xml' );
$item = $dom->getElementsByTagName( "item" );
foreach( $item as $items )
{
// Locate the Item Link (running a loop so $a will go through all the links in the feed)
$link = $dom->getElementsByTagName('link')->item($a);
// Need To Sub the old URL.
// Create The New Child
$newChild = $dom->createElement('link');
// Create A new URL to overwrite the exisiting URL in the <link> tag
$textNode = $dom->createTextNode('www.mytest.co.uk/blog/');
// Append the Child
$newChild->appendChild($textNode);
// Replace The Link
$link->parentNode->replaceChild($newChild,$link);
// Link Has Been Replaced
// Locate the GUID Item Link (running a loop so $a will go through all the links in the feed)
$link = $dom->getElementsByTagName('guid')->item($a);
// Create The Child
$newChild = $dom->createElement('guid');
// Create A new URL to overwrite the exisiting URL in the <link> tag
$textNode = $dom->createTextNode('www.mytest.co.uk/blog/');
// Append the Child
$newChild->appendChild($textNode);
// Replace The Link
$link->parentNode->replaceChild($newChild,$link);
// GUID Has Been Replaced Replaced
$a++;
}
$myXml = $dom->saveXML();
//$open = fopen (rss.xml, 'a');
// fwrite ($open, $dom->saveXML());
print <<<ENDOFTEXT
<HTML>
<Head>
<title>Example of DOMNode->replaceChild</title>
<Head>
<body>
<PRE>
$myXml
</PRE>
</body>
</HTML>
ENDOFTEXT;
What I am trying to do is simple in terms but for the love of god I cannot get it to work.
Alot of people I know use typepad Blog and I want to import the feed externally to display on the persons website, another way of doing latest news if you will.
My goal is simple, I wish to change the RSS feed's links and guid tags. I have written in the above script a way of replacing these tags and then saving the file as .xml in the directory.
My real problem is this the RSS link tags comes as
www.somewebsite.co.uk/blog/2007/?somepage.html.
I want to remove the www.somewebsite.co.uk so i am just left with
/blog/2007/?somepage.html. and then add my the new address in front of it.
I have tried and tried to return a string from the following line
$link = $dom->getElementsByTagName('link')->item($a);
but continually get an error about it cannot convert into a string. I tried to use your function in your PHP XML book.
function extractText($array){
if(count($array) <= 1){
//we only have one tag to process!
for ($i = 0; $i<count($array); $i++){
$node = $array[$i];
$value = $node->get_content();
}
return $value;
}
}
and pass the following argument to it
$link = $dom->getElementsByTagName('link');
$oldURL = extractText($link);
but to no avail same error about strings. Is there any other way of achieving what I am after. I am banging my head against a wall here I need some help.
Many Thanks
Steve C
[edited by: encyclo at 12:00 am (utc) on Jan. 14, 2007]
[edit reason] examplified [/edit]