View Full Version : Ignore spaces and line changes inside string

12-12-2008, 03:43 PM
Hi all,
I use a php script to edit and update content of an xml file. The php code is the following:

$fname = "new.xml";
$fhandle = fopen($fname,"r");
$filestring = file_get_contents($fname);

$target = '<title name="'.$row_new_to_edit['title'].'">';
$target .= '<text>'.$row_new_to_edit['text'].'</text></title>';

$newdata = '<title name="'.$_POST['title'].'">';
$newdata .= '<text>'.$_POST['text'].'</text></title>';

$newfile = str_replace($target, $newdata, $filestring);

$fhandle = fopen($fname,"w");

The script finds the $target string inside the xml file, and replaces it with the $newdata string.

The problem is that there can be -change line- and -space- characters inside he <text></text> tags. If such happens, the $target string can't be found inside the sml file. So what I need is something to make the str_replace function ignore changing lines and spaces inside strings?

How am I supposed to do that? Can you help me?

12-12-2008, 04:22 PM
I'm curious about what you're actually updating ...

Is there a reason you are not using "SimpleXML" to work with XML?

12-15-2008, 09:04 AM
Thank you for this link, I studied that page and there are useful things there, but I didn't noticed any function that will help me to update specific areas of xml file.
I mean I can search for a specific node (print_r($xmlobj->xpath("//*[level='K']"));) but how is it possible to delete it or change it's content?

Do I really have to use simpleXML and change all my code? :o
Isn't there a string function that does what I'm looking for?

12-16-2008, 12:44 AM
Technically you're not updating an XML file (as an XML file).
You're opening it as a text file, changing some text, and saving it.
PHP has no idea it's an XML file if you open it using fopen().

That leads me to believe that maybe you could focus more on
validating the string from the form, so that nobody can use
extra spaces and line feeds to begin with. Whatever they type in,
you alter it before it even gets to the replace portion.

Also, we see no form ... so we're not sure what type of thing
is being changed .... or the purpose for it. That's why I'm leaning
toward a different approach. Maybe we need to see a better example
of what you're doing.