...

View Full Version : Automatically Chane URLs



Tony M
12-01-2010, 03:10 PM
I have an XML file, I want every url that point to example1.com (in that XML file) to be rewritten as example2.com... I do not want the original xml to be edited... what I want is that My server automatically make a New xml file (that is the same as the original one) and in this xml the urls will be pointing to example2.com (and not like the original xml that has example1.com), So how can I do that?
(any suggestions are helpfully, and XML solutions is also acceptable...:thumbsup:)

Lamped
12-01-2010, 03:56 PM
Consider this pseudo code, an example of a way of doing this, but I've never used SimpleXMLIterator before...



$xml = simplexml_load_file('myfile.xml');
$iterator = new SimpleXMLIterator($xml);
while ($node = $iterator->next()) {
if ($node == 'example1.com') {
$node = 'example2.com';
}
}
file_put_contents('newfile.xml', $xml->asXML());


Something like that, anyway.

Tony M
12-01-2010, 04:06 PM
Consider this pseudo code, an example of a way of doing this, but I've never used SimpleXMLIterator before...



$xml = simplexml_load_file('myfile.xml');
$iterator = new SimpleXMLIterator($xml);
while ($node = $iterator->next()) {
if ($node == 'example1.com') {
$node = 'example2.com';
}
}
file_put_contents('newfile.xml', $xml->asXML());


Something like that, anyway.

Ok, When the newfile.xml be created? (when I access it or when I access the original file...)

Lamped
12-01-2010, 04:14 PM
Ok, When the newfile.xml be created? (when I access it or when I access the original file...)

Every time you run that code. Also note: You don't have to write it to a file. You can run that code and omit the file_put_contents, and just continue to use the $xml object with the new values.

... assuming my pseudo code works...

Tony M
12-01-2010, 04:21 PM
Ok, I tested Your code and it didn't worked (for the reason that I do not know why), what is wrong? (SimpleXML s enabled on the server)

Lamped
12-01-2010, 04:29 PM
Ok, I tested Your code and it didn't worked (for the reason that I do not know why), what is wrong? (SimpleXML s enabled on the server)

I did say it was more of rough pseudo code than a working example :p

I'll have a proper look at it now.

Lamped
12-01-2010, 04:56 PM
$xml = simplexml_load_file('myfile.xml');

function processxml($xml, $find, $replace) {
foreach($xml as $key => $node) {
if (count($node->children())) {
processxml($node, $find, $replace);
}
if ($xml->{$key}) {
@$xml->{$key} = str_replace($find, $replace, $xml->{$key});
}
}
}

processxml($xml, 'example1', 'example2');
echo($xml->asXML());

There, a find and replace in every node of the xml, excludes attributes.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum