Forum Moderators: coopster
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
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);
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);
?>
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.
(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);
}
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> )(.*)(</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
$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
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!
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> )(.*)(</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