SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preg_replace in CSV file

    Hi Guys!

    I have a CSV file in which I need to replace some text. In column 1 I have the old phrase (of which I need to replace part of it with column 3). Column 2 is not required.

    So for example here is row 1 (I have also added column 1,2,3 above each column)

    Code:
    Column 1
    $language['admin_index_page_title'] = "Welcome to Admin";
    
    Column 2	
    Welcome to Admin.	
    
    Column 3
    Админд тавтай морилно уу.
    Now what I need to do is replace the text within double quotes from column 1 with the text in column 3. So column 1 will end up like this:

    Code:
    $language['admin_index_page_title'] = "Админд тавтай морилно уу.";
    Any ideas how to do this?

    Thanks.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    preg_replace('~(.*?").*?";,(.*?),(.*?)\n~','$1$3";,$2,$3\n',$csvfilecontents);

    (COMPLETELY untested. Test before writing back to the file. I'm assuming your fields are not quote-encapsulated, are seperated by commas, and lines are delimited by newline)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why not just reassign the column in the row?

    Code:
    <?php
    $row[0] = $row[2];
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    It's not replacing the value entirely, Anthony, he wants to slice it into the space between the quotes.... essentially the CSV file is a massive Find/Replace (Needle = Row[1], Replace = Row[2], Haystack = Row[0])
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah, got you. Thanks StarLion.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •