Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2010, 03:10 PM   PM User | #1
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
Automatically Chane URLs

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...)

Last edited by Tony M; 12-01-2010 at 03:12 PM..
Tony M is offline   Reply With Quote
Old 12-01-2010, 03:56 PM   PM User | #2
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
Consider this pseudo code, an example of a way of doing this, but I've never used SimpleXMLIterator before...

PHP Code:
$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.
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 12-01-2010, 04:06 PM   PM User | #3
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
Quote:
Originally Posted by Lamped View Post
Consider this pseudo code, an example of a way of doing this, but I've never used SimpleXMLIterator before...

PHP Code:
$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...)
Tony M is offline   Reply With Quote
Old 12-01-2010, 04:14 PM   PM User | #4
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
Quote:
Originally Posted by Tony M View Post
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...
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 12-01-2010, 04:21 PM   PM User | #5
Tony M
Regular Coder

 
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Tony M is on a distinguished road
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)

Last edited by Tony M; 12-01-2010 at 04:28 PM..
Tony M is offline   Reply With Quote
Old 12-01-2010, 04:29 PM   PM User | #6
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
Quote:
Originally Posted by Tony M View Post
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

I'll have a proper look at it now.
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Old 12-01-2010, 04:56 PM   PM User | #7
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
PHP Code:
$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.
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:01 AM.


Advertisement
Log in to turn off these ads.