How do I replace this line ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    How do I replace this line ?

    Hi,

    I have some data in a flat file called test_file.xml
    that reads:

    Code:
    <?xml version="1.0" encoding="utf-8"?><marketplace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.click2sell.eu/marketplace.xsd"><product>
    And I want to change it so that it reads :

    Code:
    <?xml version="1.0" encoding="utf-8"?><marketplace><product>
    What is the best way to do this ?

    I assume I should open the file and get the contents:

    Code:
    $file_in = 'test_file.xml';
    $source = file_get_contents( $file_in  );
    Now should ıs use preg_match()

    or would str_replace()

    E.g can I have "wild characters" in the str_replace() ?


    Code:
    $source= str_replace('<marketplace xmlns*' , '<marketplace>', $source);
    
    // Write it to out file
    file_put_contents( $file_out, $source );
    Maybe I need to use preg_match()
    to do this, or preg_replace() ??

    Any advice, much appreciated :)



    .
  • rythmic
    New Member
    • Feb 2010
    • 29

    #2
    As far as I know str_replace does not accept wildcards

    1. If you're comfortable with regular expressions preg_replace should do it.. basically you'd be looking for <marketplace<sp ace>*>

    2. you could parse it using php built-in xml parser functions. If element marketplace is found and it has attributes, you would replace that with a <marketplace> element without attributes.

    info about xml parser is found here: http://www.php.net/manual/en/function.xml-parse.php

    You could go through the file line by line and when condition is not met you just feed the original line and when one is found you do a replace.

    3. Go through the document programmaticall y searching each line for
    <marketplace<sp ace>. When found, search for > ignore that part and insert the rest of the line. This is not all that time efficient but might be fun

    I would definetley go with option one though as the coding required would be significantly less. Also the regexp required is not complicated so it should be quite efficient.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      preg_replace() is overkill if you know what the string is that you need to replace.

      Code:
      $str = str_replace("<marketplace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.click2sell.eu/marketplace.xsd">", "<marketplace>", $str);

      Comment

      • jeddiki
        Contributor
        • Jan 2009
        • 290

        #4
        Thanks for reply,

        I should have said - there is only one place where this occurs and it is right at the top of the file.

        So is this what I need : ?

        Code:
        $file_in = 'test_file.xml';
        $file_out = 'test_output.xml';
        
        $source = file_get_contents( $file_in  );
        $source = preg_replace( '#\<marketplace *\>#', '<marketplace>', $source);
        
         // Write it to out file
        file_put_contents( $file_out, $source );

        Hi Markus,


        I am a bit concerned about that being so specific.

        If they change the file schema name, it won't work.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          I would recommend loading this up in a XML parser. It's less risk of accidentally messing up the markup.

          Try using the DomElement::rem oveAttribute method, or something like that.

          Comment

          Working...