tags:

views:

188

answers:

1

I'm trying to process an imported XML file and make the text in one of the nodes

<Name>SOMETHINGTOMAKELOWERCASE</Name>

lowercase

<Name>somethingtomakelowercase</Name>

So far I got:

$xml = file_get_contents($xmlfile);
$xml = preg_replace('/<Name>(.*)<\/Name>/e', '<Name>' . strtolower($1) . '</Name>',$xml); 
fwrite(fopen($xmlfile, 'wb'), $xml);

I've tried about ten different versions of the regexp, but none of them will work. Could you please point me in the right direction as to the correct regexp?

+2  A: 

Try this instead:

$xml = file_get_contents($xmlfile);
$xml = preg_replace('/<Name>(.*)<\/Name>/e', "'<Name>' . strtolower('\\1') . '</Name>'",$xml); 
fwrite(fopen($xmlfile, 'wb'), $xml);

When using the /e modifier in preg_replace, you have to pass a string of code to be evaluated as the replacement parameter, not an already-evaluated expression.

Amber
+1: That's awesome, I didn't know that!
Josh
All I needed - Kudos :)
Luke