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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Replace data in a file

    I know I posted about this before but it was awhile ago and the thread just confused everyone. Problem never got solved, gave up on it awhile so now I'll get back to it.




    What I'm trying to do is replace data in a file. Example:


    file foo.txt contains:


    <!-- 9 --> random text. <!-- /9 -->
    <!-- 14 --> some more random text. <!-- /14 -->


    Now, what I want to do is replace whats between a delimiter set. Lets say 14. So I'd want to delete it. Delimiters and all, so all thats left is:

    <!-- 9 --> random text. <!-- /9 -->


    So how would I go about this?


    Thanks
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $file file_get_contents('foo.txt');
    $rem "4"// remove delimeter with this num
    preg_replace("/<!-- $rem -->.*?<!-- \/$rem -->/"""$file); 
    i haven't tested this, just threw it together. the question mark makes the match not-greedy. i know it works in perl, and i'm hoping the preg functions match perl's RegEx syntax exactly.

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can work with that Thanks for the help bro
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Slight problem.


    I need it to work on multiple lines.

    Example:

    Like it is now it will only work if its set up like this:
    <!-- 14 --> some more random text. <!-- /14 -->


    I need it to work with multiple lines like so:

    <!-- 14 -->

    some
    more
    random

    text.

    <!-- /14 -->
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Another Option

    Here is some code that would work...a bit wordy...but it does the trick.

    -Hawk

    PHP Code:
    <?
    $strFile 
    file_get_contents($fileName);

    $id            "14"//or whatever;
    $openTag       "<!-- $id -->";
    $closeTag      "<!-- /$id -->";
    $startExtract  strpos($strFile,$openTag);
    $endExtract    strpos($strFile,$closeTag)+strlen($endExtract);

    $strTop        substr($strFile,0,$startExtract);
    $strBottom     substr($strFile,$endExtract,strlen($strFile));

    $strOutput     $strTop $strBottom;
    ?>
    Last edited by Hawkmoon; 04-01-2004 at 07:09 PM.

  • #6
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try either:
    PHP Code:
    preg_replace("/<!-- $rem -->.*?<!-- \/$rem -->/m"""$file);
    // OR
    preg_replace("/<!-- $rem -->.*?<!-- \/$rem -->/s"""$file); 
    i think one of them will allow you to match a string with newlines. when i get home, i'll check my book and let you know. this allows it to be a little less wordy for you

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    preg_replace("/<!-- $rem -->.*?<!-- \/$rem -->/s", "", $file);



    Did the trick. Thanks
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums


  •  

    Posting Permissions

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