Quantcast
Viewing all articles
Browse latest Browse all 3201

replace parts of content within a xml file

What this does is open/read the xml, search for anything that contains example.com and replace it by sub.demo.com

libxml_use_internal_errors(TRUE);
	$dom = new DOMDocument;
	$dom->Load('/home/user/public_html/newfeed.xml');
var_dump(libxml_get_errors());
	if ($dom->validate()) {
    	echo " This xml is valid!\n";
        $xmlor = '/home/user/public_html/newfeed.xml';

        // open file and prepare mods
        $fh = fopen($xmlor, 'r+');
        $data = fread($fh, filesize($xmlor));
        echo "Opening local XML..." . PHP_EOL;
        $new_data = str_replace("example.com", "sub.demo.com", $data);
        fclose($fh);

// run mods
$fh = fopen($xmlor, 'r+');
fwrite($fh, $new_data);
echo "Updated feed URL in local XML..." . PHP_EOL;
fclose($fh);

	}

It’s not outputting errors, but it also isn’t making any changes to the file.

Help is appreciated, thank you in advance.


Viewing all articles
Browse latest Browse all 3201

Trending Articles