SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2004
    Location
    UK
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Edit XML with PHP

    Hi,

    I have an XML file that needs editing before it can be upload to the server.

    I need to remove the following line from the file:
    <!DOCTYPE faq SYSTEM "\\Cob-files2\Projects\Cobweb Projects\Red Tape Buster\Content\dtd\faq.dtd">

    However, I can't always be sure than line will exist in the file and if it does I can only be sure that <!DOCTYPE will always be the same. If <!DOCTYPE exists in a file then the rest of the line needs to be deleted.

    Can anyone help?

    Thanks

    Craig

  2. #2
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you need to do is open the file for editing.
    PHP Code:
    $file fopen("$Uploadedfile""r");
    while (
    $line=fgets($file,1000)){
     
    $alltext .= $line;
    }
    fclose ($file); 
    Check if your string is in the file and remove the line
    PHP Code:
    $position strpos($alltext,"<!DOCTYPE ");
    if(
    $position){
     
    $position2 strpos($alltext,'">',$positie) + 1// first "> after <!DOCTYPE 
     
    $begPos strpos($alltext,"<!DOCTYPE ");
     
    $tmp substr($alltext,0,$begPos);
     
    $tmp .= substr($alltext,$position2);
     
    $alltext $tmp;

    than write the file:
    PHP Code:
    $handle fopen("$Uploadedfile""w");
    $filewrite fwrite($handle,$alltext);
    if (!
    $filewrite) {
     echo 
    "Cannot write to file ($filename)";
     exit;
    }
    else { 
     
    // do something else
    }
    fclose($handle); 
    haven't checked it yet, but something like this should work.
    Good luck.
    Jazz
    Last edited by jazztie; May 13, 2004 at 10:59.

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you also could do is:
    PHP Code:
    $alltext preg_replace('#<!DOCTYPE(.*?)\>#im',''$alltext); 
    Maybe a little bit faster

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But you'd need to be sure there'd only ever be one DOCTYPE

  5. #5
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    true... maybe my first post will work better than...
    or you can make the preg_replace match longer, so you'll be sure it's the right one.
    <!DOCTYPE faq SYSTEM "\\Cob-files2\Projects\...

  6. #6
    SitePoint Member
    Join Date
    May 2004
    Location
    UK
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    brilliant, worked first time.

    Thanks!!!

  7. #7
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No prob... (you could give me reputationpoint )


Bookmarks

Posting Permissions

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