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
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:
Always think the easy way, regular expression most of the times doesn't have to be hard: $txt = preg_replace("|$id.*".PHP_EOL."|",'',$txt);
$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:
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.
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
If that is your concern, then the expression I provided will also remove the newlines after the line to be removed.
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):
Just Change your preg_replace to $result = preg_replace('/673;:::(.*)\.(.*)\.(.*)\.(.*):::(.*)\s/m', '', $subject); PHP:
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):
What does PHP_EOL do and what's with the straight bar in the beginning and the end of the expression?