preg_replace leaving last digit

Discussion in 'PHP' started by MyVodaFone, May 30, 2010.

  1. #1
    I have a small problem with preg_replace its leaving the last digit in the string:

    Heres my typical text list:

    
    668;:::109.76.116.225:::5
    668;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    673;:::00.00.00.00:::7
    673;:::109.76.116.225:::5
    673;:::109.76.116.225:::5
    673;:::109.76.116.225:::5
    668;:::109.76.116.225:::4
    663;:::109.76.116.225:::4
    370;:::109.76.116.225:::4
    
    Code (markup):
    My code :

    
    $id = $_GET['dvd_entry'];
        $txt = file_get_contents('results.txt');
        $txt = trim(preg_replace('#('.$id.');:::(.*)\.(.*)\.(.*)\.(.*):::(.*)#isU','',$txt));
        file_put_contents('results.txt', $txt);
    
    Code (markup):
    Here my result after deleting ID rows 673

    
    668;:::109.76.116.225:::5
    668;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    7
    5
    5
    5
    668;:::109.76.116.225:::4
    663;:::109.76.116.225:::4
    370;:::109.76.116.225:::4
    
    Code (markup):
    As you can see the last digit in each row gets left behind, can anyone spot what I doing wrong ?

    EDIT:

    Seems I didn't need a /modifier after all so its all work as is
     
    Last edited: May 30, 2010
    MyVodaFone, May 30, 2010 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    Replacing #isU with $#misU should be a quick fix.
     
    joebert, May 30, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    nah still left white-spaces

    trim() not functioning
     
    MyVodaFone, May 30, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    In that case, how about moving from file_get_contents() to file() and just checking the part which differs ?

    $arr = file('results.txt');
    
    for($i = count($arr); $i > -1; $i--)
    {
        if($id == substr($arr[$i], 0, strlen($id)))
        {
            unset($arr[$i]);
        }
    }
    
    file_put_contents('result.txt', implode($arr));
    PHP:
     
    joebert, May 30, 2010 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Always think the easy way, regular expression most of the times doesn't have to be hard:

    $txt = preg_replace("|$id.*".PHP_EOL."|",'',$txt);
     
    ThePHPMaster, May 31, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    
        $txt = file_get_contents('results.txt');
        $txt = preg_replace("|$id.*".PHP_EOL."|",'',$txt); // removes the required line, thanks @ThePHPMaster
        $txt = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $txt); // removes empty lines
        file_put_contents('results.txt', $txt);
    
    PHP:
     
    MyVodaFone, May 31, 2010 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    FYI, if your file does not originally contain unwanted newlines, you do not need that extra preg_replace for the newlines. That is unless you are doing other operations which might leave newlines.
     
    ThePHPMaster, May 31, 2010 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    There are many lines all different id numbers and duplicate id numbers

    1
    2
    3
    2
    2
    4
    5 etc..

    removing example 2 was leaving empty lines so I was trying the trim function, however its just a easy for now to run a second preg_replace
     
    MyVodaFone, May 31, 2010 IP
  9. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #9
    If that is your concern, then the expression I provided will also remove the newlines after the line to be removed.
     
    ThePHPMaster, May 31, 2010 IP
  10. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #10
    preg_replace isn't the right tool for this.
     
    joebert, May 31, 2010 IP
  11. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Try this please:

    
    <?php
    $subject =<<<AAA
    668;:::109.76.116.225:::5
    668;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    673;:::00.00.00.00:::7
    673;:::109.76.116.225:::5
    673;:::109.76.116.225:::5
    673;:::109.76.116.225:::5
    668;:::109.76.116.225:::4
    663;:::109.76.116.225:::4
    370;:::109.76.116.225:::4
    AAA;
    
    $result = preg_replace('/673;:::(.*)\.(.*)\.(.*)\.(.*):::(.*)\s/m', '', $subject);
    
    echo "\n$result\n";
    
    PHP:
    Output:
    
    668;:::109.76.116.225:::5
    668;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    663;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    370;:::109.76.116.225:::5
    668;:::109.76.116.225:::4
    663;:::109.76.116.225:::4
    370;:::109.76.116.225:::4
    
    Code (markup):
     
    flexdex, May 31, 2010 IP
  12. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #12
    Just Change your preg_replace to

    
    $result = preg_replace('/673;:::(.*)\.(.*)\.(.*)\.(.*):::(.*)\s/m', '', $subject);
    
    PHP:
     
    roopajyothi, Jun 1, 2010 IP
  13. kidatum

    kidatum Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I used preg_match for testing purposes but the regex is just what you need except it doesn't include white spaces and new lines.

    $text = "668;:::109.76.116.225:::5";
    
    preg_match('/[0-9]{1,3}[;][:]{3,3}[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[:]{1,3}[0-9]/', $text, $match);
    Code (markup):
    If you want white spaces and new lines included just place Ss after the last forward slash, like this:

    $text = "668;:::109.76.116.225:::5";
    
    preg_match('/[0-9]{1,3}[;][:]{3,3}[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[:]{1,3}[0-9]/Ss', $text, $match);
    Code (markup):
     
    kidatum, Jun 1, 2010 IP
  14. kidatum

    kidatum Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    What does PHP_EOL do and what's with the straight bar in the beginning and the end of the expression?
     
    kidatum, Jun 1, 2010 IP