Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace data into text file

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
I have a table contains 'id' and 'date' and I use accessdump software to convert an MS Access table to dump file "news.txt" the result was for example:

INSERT INTO news VALUES('2','20/01/2004');
INSERT INTO news VALUES('5','07/01/2004');
etc……

But what I need is: replace the " INSERT INTO news VALUES('2','20/01/2004');" into " UPDATE news set date ='20/01/2004' where id = 2 ; "
Any ideas?
Thanks in advance
 
Code:
<?

$strings = "INSERT INTO news VALUES('2','20/01/2004');
INSERT INTO news VALUES('5','07/01/2004');";

$strings = explode("\n", $strings);
$stringsOUT = "";
$i = 0;

 for ($i=0;$i <= count($strings)-1;$i++) {
  $stringsOUT .= preg_replace("'INSERT INTO news VALUES\(\'(.*?)\'\,\'(.*?)\'\)\;'","UPDATE news set date ='\$2' where id = '\$1' ;", $strings[$i]);  
 }

print $stringsOUT;

?>

this *should* work (works on my XP/Apache1.3.24/PHP4.3.4) ;)

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Thank you jamesp0tter,
Your code work perfectly, but when I tried to read the text file as:

<?
$file = file ('../path to/news.txt');
foreach($file as $line) {
$strings= $strings.$line;
}

$strings = explode("\n", $strings);
$stringsOUT = "";
$i = 0;
for ($i=0;$i <= count($strings)-1;$i++) {
$stringsOUT .= preg_replace("'INSERT INTO news VALUES\(\'(.*?)\'\,\'(.*?)\'\)\;'","UPDATE news set date ='\$2' where id = '\$1' ;", $strings[$i]);
}
print $stringsOUT;

?>

This returns the original data of news.txt without any modification.
 
'cuz that $strings= $strings.$line; is creating a single-lined string.. use $strings .= $line."\n"; instead.

Code:
<?
$stringsOUT = "";

$file = file ('../path to/news.txt');
foreach($file as $line) {
  $stringsOUT .= preg_replace("'INSERT INTO news VALUES\(\'(.*?)\'\,\'(.*?)\'\)\;'","UPDATE news set date ='\$2' where id = '\$1' ;", $line);  
}

print $stringsOUT;

?>

this will work (i hope lol, don't have time to test it now) ;)

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top