Forum Moderators: coopster

Message Too Old, No Replies

Find and Replace

Find a string and use it to replace another

         

coho75

4:19 am on Feb 27, 2004 (gmt 0)

10+ Year Member



I need a script that can locate a string in a file (same line/position in every file) copy it and replace another string in the same file with the copied string.

open a pointer to a page
grab the text from <td>Text to copy</td>
replace zzz with "Text to copy"
close the pointer

I think this can be done with PHP. I just don't know where to start. Any help is greatly appreciated!

Coho75

superpower

5:01 am on Feb 27, 2004 (gmt 0)

10+ Year Member



I think this is the concept you are looking for.

It may not work exactly like this (I didn't test it) but it will get you in the right direction.

$filename = "foo.html";
$fd = fopen ($filename, "r");
$str = fread ($fd, 999999);
fclose ($fd);

//make the next line as specific as possible because it;s greedy and will match everything I believe (if you have other td's in the file)

eregi("(<td>)(.*)(</td>)",$str,$out);

$found = $out[2];

// $out[2] is what was between the td tags

// after this try using the str_replace function

$zzz = "zzz";
$new_file_text = str_replace("$found","$zzz",$str);

// Write file

$filePointer = fopen($filename,"w");
fwrite($filePointer, $new_file_text);
fclose($filePointer);

coho75

5:04 am on Feb 27, 2004 (gmt 0)

10+ Year Member



Thanks SuperPower. I will give the code a shot and see how it works out. At least I have a better idea of what direction I need to be headed in!

Thanks Again,
Coho75

coho75

6:26 am on Feb 27, 2004 (gmt 0)

10+ Year Member



I am having some trouble making this work. I think that the problem is the regular expressions. It takes the original term (zzz) and removes it, but it doesn't replace it with the text that is supposed to be captured from between the <td>s. Would it be possible to perform this action on a bunch of files from a list or something?

The Code I have is below:

<?php

$filename = "recipeC32060.html";
$fd = fopen ($filename, "r");
$str = fread ($fd, 999999);
fclose ($fd);

//make the next line as specific as possible because it;s greedy and will match everything I believe (if

you have other td's in the file)

eregi("<td class=spFrameTitle> . </td>", $str , $out);

$found = $out[2];

// $out[2] is what was between the td tags

// after this try using the str_replace function

$oldtext = "zzz";
$new_file_text = str_replace($oldtext , $found , $str);

// Write file

$filePointer = fopen($filename,"w");
fwrite($filePointer, $new_file_text);
fclose($filePointer);

?>

superpower

6:46 am on Feb 27, 2004 (gmt 0)

10+ Year Member



Yes, it looks like your regex is messed up... be very careful because with the reg ex everything has to be in place perfectly

You need the parentheses. So:

eregi("(stuff before what you want)(.*)(stuff after what you want)",$str , $out);

echo "Before:<br><br>$out[1]<br><br>";
echo "The good stuff:<br><br>$out[2]<br><br>";
echo "After:<br><br>$out[3]<br><br>";

See if that works.

superpower

7:15 am on Feb 27, 2004 (gmt 0)

10+ Year Member



I almost forgot about your other question...list of files: you can either

(1) put all the files/path in an array and foreach loop through all this code for each one.

(2) if you have all the files in one directory...

Something like:

if ($handle = opendir('/domains/mypath/files/')) {
while (false!== ($filename = readdir($handle))) {
if ($filename!= "." && $filename!= "..") {

// (all the other code goes here)

}

}
closedir($handle);
}

coho75

3:26 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Ok, I got the regex working for individual files. When I try to iterate through my text file I have no luck. The script is grabbing the filenames correctly, but it isn't going through the php inside of the { }. Should I be assigning $zz to $filename? Foreach loops are still not making sense to me.

This is what I have so far --

<?php

$datalines = file('test.txt');

foreach($datalines As $zz) {

$filename = "$zz";
// echo "$filename";
$fd = fopen ($filename, "r");
$str = fread ($fd, 999999);
fclose ($fd);

//make the next line as specific as possible because it;s greedy and will match everything I believe (if

you have other td's in the file)

ereg("(<TD class=spFrameTitle>&nbsp;&nbsp;)(.*)(</TD>)",$str , $out);

// echo "Before:<br><br>$out[1]<br><br>";
// echo "The good stuff:<br><br>$out[2]<br><br>";
// echo "After:<br><br>$out[3]<br><br>";

$found = $out[2];

// $out is what was between the td tags

// after this try using the str_replace function

$oldtext = "zzz";
$new_file_text = str_replace($oldtext , $found , $str);

// Write file

$filePointer = fopen($filename,"w");
fwrite($filePointer, $new_file_text);
fclose($filePointer);
}

?>

Thanks,
Coho75

superpower

5:33 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I think you combined two separate array ideas I mentioned. What I meant by foreach through an array of files is like this (at most basic, see further for a better idea):


$all_files = array('test.txt','test2.txt','test3.txt');

foreach($all_files as $filename) {
// rest of the code
}

The file() function is used for reading the contents of one file into an array (the array is each line). You can do that too but it would mean changing other parts of the code I gave you. I don't think it's necessary for what you are trying to do.

However, if you wanted to get a tiny bit fancier on the file list, you could do something like have a file with a list of all the files you want (each on a new line) and then read that into an array, then foreach through it. ie.

$file_list = file('my_list.txt');
foreach ($file_list as $filename){
// the rest of the code
}

coho75

6:05 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



Thanks again SuperPower for your help. I have a file with all of the file names in it (several thousand). So I need to read each HTML file name from the list and run it through the code, then grab the next file and run it through the code, and so on. I thought I could do it the way I had the foreach loop set up, but it grabs the filename but doesn't run it through the code. Is my logic correct?

Coho75

superpower

6:31 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I think you're right.

Make sure to back up everything of course because my experience of processing a large amount files is that things go wrong sometimes. Instead of overwriting the file you probably want to just write a new file in a separate directory. Then when everything checks out ok just mv the entire directory over (or do something similar if feasible). Good luck!

coho75

8:03 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



I am trying to foreach through a text file (each entry on a new line) with no luck. I get the script to grab the names of the file without any problems, but when I try to place the file name into the code ($filename = $zz) it doesn't seem to work and I can't figure it out. I have been messing with it for about 5 hours now. All help is greatly appreciated.

My Code:

<?php

$data = file('test.txt');

foreach($data As $zz) {

$filename = $zz;
echo "$zz";
// prints the right file names -- one after the other
$fd = fopen($zz, "r");
$str = fread($fd, 999999);
fclose($fd);

//make the next line as specific as possible because it;s greedy and will match everything I believe (if

you have other td's in the file)

ereg("(<TD class=spFrameTitle>&nbsp;&nbsp;)(.*)(</TD>)",$str , $out);

// echo "Before:<br><br>$out[1]<br><br>";
// echo "The good stuff:<br><br>$out[2]<br><br>";
// echo "After:<br><br>$out[3]<br><br>";

$found = $out[2];

// $out is what was between the td tags

// after this try using the str_replace function

$oldtext = "zzz";
$new_file_text = str_replace($oldtext , $found , $str);

// Write file

$filePointer = fopen($zz,"w");
fwrite($filePointer, $new_file_text);
fclose($filePointer);
}

?>

Coho75