Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    106
    Thanks
    27
    Thanked 3 Times in 3 Posts

    Ignore spaces and line changes inside string

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

    Code:
    $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");
    fwrite($fhandle,$newfile);
    fclose($fhandle);
    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?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,492
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I'm curious about what you're actually updating ...

    Is there a reason you are not using "SimpleXML" to work with XML?
    http://www.webmonkey.com/tutorial/Ge..._Object_as_XML

  • Users who have thanked mlseim for this post:

    ktsixit (12-15-2008)

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    106
    Thanks
    27
    Thanked 3 Times in 3 Posts
    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?

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

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,492
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •