I am trying to write to an xml file. All have same ending.

<gallery id="1">
<img src=blabla>
</gallery>

<gallery id="2">
<img src=blabla>
</gallery>

<gallery id="3">
<img src=blabla>
</gallery>

when I do

fread(fopen("file.xml", "r"), 100000);
$start= strpos($fd,"<gallery id=\"2\">";

I get the pointer position. Now I need to get the corresponding </galley>

when I do

$finish=strpos($fd,"</gallery>");

of course, it picks up the </gallery> from above because the cursor starts at 0 again.

How can I tell $finish to start looking for "</gallery>" from $start and to write in that space? This is the one I need to replace by writing over it.

Thanks.

    Installer wrote:

    Or you can use the XML functions designed for the purpose.

    Please, can you give me a pointer on how to use that?

    thanks.

      Here's an example:

      $str = <<<XML
      <?xml version="1.0" standalone="yes"?>
      <top>
      <gallery id="1">
      <img src="test_1.jpg" />
      <txt>
      hello
      </txt>
      </gallery>
      <gallery id="2">
      <img src="test_2" />
      </gallery>
      <gallery id="3">
      <img src="test_3.jpg" />
      </gallery>
      </top>
      XML;
      
      $xml = new SimpleXMLElement($str);
      
      // Before changes:
      echo '<pre>';
      echo htmlentities($xml->asxml());
      echo '</pre>';
      
      foreach ($xml->gallery as & $each) {
          // Read element and attribute values:
          echo $each->txt . '<br />';
          echo $each->img['src'] . '<br />';
          $id = $each['id'];
          echo $id . '<br />';
          if ($each['id'] == 1) {
              // Change element and attribute values:
              $each->txt = 'goodbye';
              $each->img['src'] = 'test.jpg';
          }
      }
      
      // After changes:
      echo '<pre>';
      echo htmlentities($xml->asxml());
      echo '</pre>';
      
      // Write to file:
      $xml->asxml('xml_test.xml');

      SimpleXML functions

        Thanks. I don't want to write entire xml over. I have 11 galleries with 20 images each. What I wanted to do, is when ONE gallery is updated to update only that one in the xml.

          Well, that's the way to do it. (The XML a the top of the script is only for using in the test script. For real use you would pass the path to the file in the "new" statement, make your specific changes, and write the XML back to the file. Backup. See the SimplXML section of the manual for lots of examples.)

          If you have a gallery id number, and the id numbers started at zero and were contiguous, in order, then you could access your targeted ones by using the id as offset, for instance, "$xml->gallery[2]". Otherwise you can loop through them and use "if", as in my example.

            How about exploding it into an array on </gallery>, overwrite the array element then implode it back into a string

            $gal = explode($fd, '</gallery>');
            foreach ($gal as $val) {
               if (strpos($val,'<gallery id="2">')) {
                   // do your re-write
               }
            }
            // then implode it back into the file
            

              Thank you. I appreciate your help.
              here is what I am trying to accomplish. Everytime a new thread is started in a preset forum, if there are images attached to it, then I query to create the new xml. I was trying to avoid to query all the different forums, but hey, if you say this is the way, then this is the way.

              P.S. They don't actually have ID number, but names.

                This is not doing anything. When I try to print the $gals array, it's empty. Please can someone help me on that?

                $gal="women";
                $galtitle="Women";
                $galdesc="Articles tips gossips recipes";
                
                $handle="/var/www/slideshow/slideshow_data0.xml";
                $fd= fread(fopen($handle, "r"), 1000000); 
                $gals = explode($fd, '</gallery>'); 
                print_r($gals);
                
                exit;
                foreach ($gal as $val) { 
                
                if (strpos($val,'<gallery id="$gal" title="$galtitle" description="$galdesc">')) {
                $_xml="<gallery id=\"$gal\" title=\"$galtitle\" description=\"$galdesc\">";
                $_xml.="<img align=\"center\" src=\"/forums/attachment.php?attachmentid=7700\" 
                
                thumb=\"/forums/attachment.php?attachmentid=7700\" />";
                }
                implode($val, $_xml);
                }
                
                 fclose($fd);
                echo "success";

                  Whoops, sorry, syntax error

                  $gals = explode('</gallery>', $fd); 
                  

                  Mind you it should have returned '</gallery>' cos $fd is not in it??

                    Are you sure about the rest of that code? Does not look like you are overwriting that array element, or reconstructing the file contents once you have. Still one step at a time eh.

                      Roger Ramjet wrote:

                      Are you sure about the rest of that code? Does not look like you are overwriting that array element, or reconstructing the file contents once you have. Still one step at a time eh.

                      I've been at it for 2 days and 2 nights. Here is the full code. Any help is greatly appreciated. I can handle the db query to get information, once I am passed writing the tests

                      <?php
                      
                      $gal="women";
                      $galtitle="Women";
                      $galdesc="Articles tips gossips recipes";
                      
                      $handle='/var/www/slideshow/slideshow_data0.xml';
                      
                      $fd= fread(fopen($handle, "r"), 10000000); 
                      
                      $gals = explode('</gallery>',$fd); 
                      
                      foreach ($gals as $val) { 
                      
                      if (strpos($val,'<gallery id="$gal" title="$galtitle" description="$galdesc">')) {
                      $_xml="<gallery id=\"$gal\" title=\"$galtitle\" description=\"$galdesc\">";
                      $_xml.="<img align=\"center\" src=\"/forums/attachment.php?attachmentid=7700\" 
                      
                      thumb=\"/forums/attachment.php?attachmentid=7700\" />";
                      }
                      fwrite($_xml,$fd);
                      }
                      
                       fclose($fd);
                      
                      echo "success";
                      
                      ?>

                        and here is one complete nod

                        <gallery id="women" title="Women" description="Articles tips gossips recipes">
                        <img align="center" src="/forums/attachment.php?attachmentid=7738" thumb="/forums/attachment.php?attachmentid=7738" title="This and that"  caption="Insert intro here" />
                        <img align="center" src="/forums/attachment.php?attachmentid=7787" thumb="/forums/attachment.php?attachmentid=7787" title="This other title"  caption="Insert intro here" />
                        </gallery>
                        

                          OK, thought you could fill in the details

                          $gals = explode('</gallery>',$fd);
                          
                          foreach ($gals as $key=>$val) {
                             if (strpos($val,'<gallery id="$gal" title="$galtitle" description="$galdesc">')) {
                                $gals[$key] = '<gallery id="$gal" title="$galtitle" description="$galdesc">
                                    <img align="center" src="/forums/attachment.php?attachmentid=770" thumb="/forums/attachment.php?attachmentid=7700" />';
                                break 2;
                             }
                          }
                          
                          $fd = implode('</gallery>', $gals);
                          

                            sorry, but nothing is happening in the xml. It remained unchanged

                              Cos I did not concatenate the vars in the strings when I took out the double quotes - I just hate all those \"

                                I don't think the strpos() is finding it.
                                I did

                                foreach ($gals as $key=>$val) {
                                if (strpos($val,'<gallery id="$gal" title="$galtitle" description="$galdesc">')) {

                                echo "got it";

                                exit;

                                and that was ignored.

                                  Getting somewhere with concatenation.

                                  Now, I don't pass the "break 2;" point. I tried removing the "2", it ignored the "break;" and kept on going. Nothing was written. I tried removing the "break;" altogether, nothing still

                                    if (eregi('<gallery id="' . $gal . '" title="' . $galtitle . '" description="' . $galdesc . '">', $val)) { 
                                    

                                    Now you will have to learn how to debug. Easy way is just to insert echos that track where you are.

                                    The break 2 means break out of the IF and the FOREACH, it just saves unecessary processing after the if is true.

                                    Of course the if is not working with strpos - should be eregi. I'm just pasting your code without looking at it too closely, you've got to get some things right for yourself.