preg_replace ucwords strtolower help

Associate
Joined
18 Oct 2002
Posts
100
Hi

I'm trying to use preg_replace ucwords strtolower to convert an XML file with 100s of different names in upper case to ucwords strtolower
Can't get it to work :(

had a look at the manual but trying to understand the principle of how it works

I know this example is incorrect ...
eg

$filename = "name.xml";
$data = preg_replace("<name>(.*?)</name>", "<name>ucwords (strtolower(.*?));</name>", file_get_contents($filename));
$fp = fopen($filename, "w");
fwrite($fp, $data);
fclose($fp);

eg
<name>JOE BLOGGS</name>
<name>FRED JONES</name>
to
<name>Joe Bloggs</name>
<name>Fred Jones</name>

Can anyone point me in the right direction?

Thanks in advance
 
Associate
Joined
5 Jun 2004
Posts
515
Location
Cambridge
Code:
<?php

$file = "<name>JOE BLOGGS</name>\n".
	"<name>FRED JONES</name>";
		
$result = preg_replace('/<name>(.*)<\/name>/Uie', "'<name>'.ucwords(strtolower('$1')).'</name>'", $file);

echo htmlentities($result);
?>

That's what I came up with just now, obviously it doesn't do all the file handling stuff.

EDIT: BTW, I should imagine you'd need to be careful with using the 'e' modifier though. Perhaps a PHP Guru could comment?
EDIT2: Why not just parse the XML? If you're using PHP5 it's super easy, if not XML_Serializer might be the easiest way of doing things.
 
Last edited:
Associate
OP
Joined
18 Oct 2002
Posts
100
Pine said:
Code:
<?php

$file = "<name>JOE BLOGGS</name>\n".
	"<name>FRED JONES</name>";
		
$result = preg_replace('/<name>(.*)<\/name>/Uie', "'<name>'.ucwords(strtolower('$1')).'</name>'", $file);

echo htmlentities($result);
?>

That's what I came up with just now, obviously it doesn't do all the file handling stuff.

EDIT: BTW, I should imagine you'd need to be careful with using the 'e' modifier though. Perhaps a PHP Guru could comment?
EDIT2: Why not just parse the XML? If you're using PHP5 it's super easy, if not XML_Serializer might be the easiest way of doing things.


Works great many thanks :)

I'll take a look at XML_Serializer
 
Associate
Joined
5 Jun 2004
Posts
515
Location
Cambridge
slasher said:
Works great many thanks :)

I'll take a look at XML_Serializer

Using XML_Serializer was as an alternative to doing it with regular expressions and the 'e' modifier. Using the 'e' modifier might be a security risk, I'm not entirely sure on this one.

*Waits for someone more knowledgeable*
 
Back
Top Bottom