mr.echo Posted December 24, 2008 Share Posted December 24, 2008 hi guys,i have problems about make changes in txt.i want to delete same numbers at my text but i couldn't find a way to do that. first of all it is my code <?php $filename = 'input.txt'; $fp = fopen( "input.txt" , "r" ); $str = fread( $fp, filesize($filename)); $exp = explode('|',$str); $dataset = array(); foreach ($exp as $key => $val) { if($key%5 == 0) $dataset[$val] = array('code'=>$val, 'name'=>$exp[$key+1],'credit'=>$exp[$key+2],'need code'=>$exp[$key+3],'explanation'=>$exp[$key+4]); } ksort($dataset); echo ' <style> .DersTablo { border:1px solid black; } .DersTablo td { text-align:center; border:1px solid black; margin:0; padding:0 } .DersTablo th { text-align:center; border:1px solid black; margin:0; padding:0 } </style> <table width="100%" class="DersTablo"> <tr> <th>code</th> <th>name</th> <th>credit</th> <th>need code</th> <th>explanation</th> <tr> '; foreach ($dataset as $k => $row) { echo '<tr>'; foreach ($row as $v => $field) echo '<td>'.$field.'</td>'; echo '</tr>'; } echo '</table>'; ?> and i attached the txt file see, i did the sorting but i want to delete the same lesson codes and its values -credit,explanation etc..- and save it to a txt file.so my question is what functions can make this issue done? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/ Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 I'm not 100% sure what your asking but if you wanted to delete the ELM2131 (record) you could do something like this <?php $filename = 'input.txt'; $fp = fopen( "input.txt" , "r" ); $str = fread( $fp, filesize($filename)); //Remove the BIM2111 record $code= "BIM2111"; $str = preg_replace('/('.$code.'(?:\|[^|]*))\|/sim', '', $str); // you may want to write it back to the file (not sure, of the goal) Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723199 Share on other sites More sharing options...
mr.echo Posted December 24, 2008 Author Share Posted December 24, 2008 I'm not 100% sure what your asking but if you wanted to delete the ELM2131 (record) you could do something like this i m asking that, for example we have two same record, we got two ELM2131 records,and we have to delete one of them completely i mean the lesson code,explanation, credit parts, all of it. so the code must search whole text and look for matches. i hope that i told my problem clear :S Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723205 Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 Okay heres an update.. it allows you to remove any code manually, but also automatically removes any dups.. <?php $str = file_get_contents('input.txt'); $lines = preg_split('/\r\n/', $str); $code= "ELM2131"; //Manual Delete $NewFile = ""; $dirty = false; $dataset = array(); foreach($lines as $line) { if (!preg_match('/^\|'.$code.'\|/i', $line)) { preg_match('/\|?([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)/sim', $line, $result); $dataset[$result[1]]= array($result[1], $result[2], $result[3], $result[4], $result[5]); //Only Use the last code $NewFile[$result[1]] = $line; }else{ $dirty = true; } } if($dirty) { //write back $NewFile = implode("\r\n",$NewFile); file_put_contents('input2.txt',$NewFile); $str = $NewFile; } ksort($dataset); echo ' <style> .DersTablo { border:1px solid black; } .DersTablo td { text-align:center; border:1px solid black; margin:0; padding:0 } .DersTablo th { text-align:center; border:1px solid black; margin:0; padding:0 } </style> <table width="100%" class="DersTablo"> <tr> <th>code</th> <th>name</th> <th>credit</th> <th>need code</th> <th>explanation</th> <tr> '; foreach ($dataset as $k => $row) { echo '<tr>'; foreach ($row as $v => $field) echo '<td>'.$field.'</td>'; echo '</tr>'; } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723255 Share on other sites More sharing options...
mr.echo Posted December 24, 2008 Author Share Posted December 24, 2008 Okay heres an update.. it allows you to remove any code manually, but also automatically removes any dups.. that works good thanks a lot,and also i modified your code a little bit,created a form and $code= $_POST['text'] so i can delete it more easily, actually that is not what i dreamed it,i want to make the program delete automatically but anyways thanks for your time but if you have a tip for that please let me know cheers Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723283 Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 It should be deleting dups automatically.. But it saves them in input2.txt you could change that to input.txt at your own risk Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723303 Share on other sites More sharing options...
mr.echo Posted December 24, 2008 Author Share Posted December 24, 2008 It should be deleting dups automatically.. But it saves them in input2.txt you could change that to input.txt at your own risk no no i mean that if program find two same records,-means need to compare all records-,it could delete it automatically,but i guess it is so difficult for me Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723314 Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 you could make it display the dups.. as for auto deleting at the moment it only keeps the last one found.. but without knowing conditions thats the easiest option.. Heres a quick version to show dups.. (kinda a rush job but you get the idea) <?php $str = file_get_contents('input.txt'); $lines = preg_split('/\r\n/', $str); $code= "ELM2131"; //Manual Delete $NewFile = ""; $dirty = false; $dataset = array(); $dups = array(); foreach($lines as $line) { if (!preg_match('/^\|'.$code.'\|/i', $line)) { preg_match('/\|?([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)/sim', $line, $result); if(count($result)>0) { if(isset($dataset[$result[1]])) { $dups[$result[1]][0]= $dataset[$result[1]]; $dups[$result[1]][]= array($result[1], $result[2], $result[3], $result[4], $result[5]); } $dataset[$result[1]]= array($result[1], $result[2], $result[3], $result[4], $result[5]); //Only Use the last code $NewFile[$result[1]] = $line; } }else{ $dirty = true; } } if($dirty) { //write back $NewFile = implode("\r\n",$NewFile); file_put_contents('input2.txt',$NewFile); $str = $NewFile; } ksort($dataset); echo ' <style> .DersTablo { border:1px solid black; } .DersTablo td { text-align:center; border:1px solid black; margin:0; padding:0 } .DersTablo th { text-align:center; border:1px solid black; margin:0; padding:0 } </style> <table width="100%" class="DersTablo"> <tr> <th>code</th> <th>name</th> <th>credit</th> <th>need code</th> <th>explanation</th> <tr> '; foreach ($dataset as $k => $row) { echo '<tr>'; foreach ($row as $v => $field) echo '<td>'.$field.'</td>'; echo '</tr>'; } echo '</table>'; ksort($dups); echo "<pre>";print_r($dups); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723335 Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 Okay forget that.. after i re-read your post i get what you mean now.. try this <?php $str = file_get_contents('input.txt'); $lines = preg_split('/\r\n/', $str); $NewFile = ""; $dirty = false; $dataset = array(); $dups = array(); foreach($lines as $line) { if (!isset($dups[$line])) { $dups[$line] = true; preg_match('/\|?([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)\|([^|]*)/sim', $line, $result); if(count($result)>0) { $dataset[$result[1]]= array($result[1], $result[2], $result[3], $result[4], $result[5]); //Only Use the last code $NewFile[$result[1]] = $line; } }else{ $dirty = true; } } if($dirty) { //write back $NewFile = implode("\r\n",$NewFile); file_put_contents('input2.txt',$NewFile); $str = $NewFile; } ksort($dataset); echo ' <style> .DersTablo { border:1px solid black; } .DersTablo td { text-align:center; border:1px solid black; margin:0; padding:0 } .DersTablo th { text-align:center; border:1px solid black; margin:0; padding:0 } </style> <table width="100%" class="DersTablo"> <tr> <th>code</th> <th>name</th> <th>credit</th> <th>need code</th> <th>explanation</th> <tr> '; foreach ($dataset as $k => $row) { echo '<tr>'; foreach ($row as $v => $field) echo '<td>'.$field.'</td>'; echo '</tr>'; } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723336 Share on other sites More sharing options...
mr.echo Posted December 24, 2008 Author Share Posted December 24, 2008 works awesome thank you so much for your help,you're the one Quote Link to comment https://forums.phpfreaks.com/topic/138312-solved-delete-same-code-numbers-and-save-the-text/#findComment-723362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.