Jump to content

[SOLVED] delete same code numbers and save the text?


Recommended Posts

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]

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)

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

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>';

?>

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

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 :)

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);
?>

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>';
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.