Results 1 to 6 of 6

Thread: deleting a line containing a string (help)

  1. #1
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default deleting a line containing a string (help)

    Hello

    I opened a file in a variable
    and I'm searching for the code to delete (or replace with blank) a line
    which contain a specific word/string, the rest of the line is generated and can be different each time.

    example :
    assuming :
    - what is generated randomly is in red
    - what i search is in green (and it normaly seen only once in the file)

    $file = "1031hc023blablabla461hjk0456"

    How i can delete this line ?? (from the string and not the file/string).
    Is there a simple code line ? or should i count the line as if it was a file ??
    Can i use something like : str_replace or preg_replace ??
    I couldn't find an example doing this on the web

    someone help ?
    Last edited by sfchun; 04-29-2008 at 11:24 AM.

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    I think this is what you're looking for:
    PHP Code:
    str_replace("blablabla"""$file); 

  3. The Following User Says Thank You to Medyman For This Useful Post:

    sfchun (04-29-2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default

    if no mistake, this will replace a part of the string, by nothing,
    but the rest of the line will stay.

    what i need is to delete the the full line once the 'blablabla' had been found

  5. #4
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Oh, I see now...

    The below will do the trick. There are probably other ways do to it. More knowledgable folk might have different recommendations:

    PHP Code:
    <?php 

    $file 
    "1031hc023blablabla461hjk0456";
    if (
    substr_count($file'blablabla') > 0) {
        
    $file "";
    }

    ?>
    This bascially test to see if 'blablabla' is within the string. If it is, it wipes the variable clean.

  6. The Following User Says Thank You to Medyman For This Useful Post:

    sfchun (04-29-2008)

  7. #5
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default

    it's not exactly what i need, cause in fact my $file variable, contains more than only one line ...

    if i use this trick it removes not only the line but also the rest of the variable

  8. #6
    Join Date
    Apr 2008
    Posts
    58
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default

    I changed my way of reading the file at the beginning (even if it's not what i wanted to do at first...
    but i was wrong in a sens ... my variable was containing ONE line (with all the text) only, so i couldn't logically ask PHP to remove a specific line in it -_-;;;

    so rather that get the whole file in a $variable
    i read the file lines in an array[],
    then i use your trick to test the line and replace it by blank if needed...

    now it works...

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
  •