Forum Moderators: coopster
<?php
$filename = "test.html";
####################################
// Read file to change
$fp = fopen($filename, "r") or die("Couldn't open $filename");
$str = fread($fp, 10000);
fclose($fp);
ereg("([^0-9]--[ \t\n\r]DeleteBegin[ \t\n\r]--[^0-9])([^0-9]--[ \t\n\r]DeleteEnd[ \t\n\r]--[^0-9])", $str, $out);
$found = "Test";
// $out is what should be between the <!-- DeleteBegin --> and <!-- DeleteEnd -->
$oldtext = "$out[2]";
$new_file_text = str_replace($oldtext , $found , $str);
// Write file
$filePointer = fopen($filename,"w");
fwrite($filePointer, $new_file_text);
fclose($filePointer);
?>
Maybe I am trying to do something that could be done an easier way or something that may not be possible. All help is appreciated.
Thanks,
coho75
ereg("([^0-9]--[ \t\n\r]DeleteBegin[ \t\n\r]--[^0-9])([^0-9]--[ \t\n\r]DeleteEnd[ \t\n\r]--[^0-9])", $str, $out);
personally I prefer the preg (Perl-compatible regular expression ) syntax, and I'd do something like this:
$str = preg_replace('/<!--\s*DeleteBegin\s*-->.*?<!--\s*DeleteEnd\s*-->/','',$str);
This matches and replaces in one go. See: [php.net...] (Disclaimer: code untested).
You might also want to be sure you're reading in the whole of your file, not just the first 10000 bytes (unless you're sure the file won't exceed that).
$str = preg_replace('/<!--\s*DeleteBegin\s*-->(.*?)<!--\s*DeleteEnd\s*-->/', "$1", $string);
1ovector:
Captured substrings are returned to the caller via a vector of integer offsets whose address is passed in ovector.
Resource: PCRE man pages [pcre.org]
$str = preg_replace('/<!--\s*DeleteBegin\s*-->(.*?)<!--\s*DeleteEnd\s*-->/', "$1", $string);
This has the effect of deleting the comment tags DeleteBegin / DeleteEnd, but not the characters inbetween, which is not what coho75 wanted (if I understand correctly).
The sections of text being deleted could be printed using preg_replace_callback() like this:
$string = <<<_END
Keep me <!-- DeleteBegin --> but delete me <!-- DeleteEnd --> and keep me too
_END;print "Original:\n$string\n";
$string = preg_replace_callback('/<!--\s*DeleteBegin\s*-->.*?<!--\s*DeleteEnd\s*-->/','show_deleted',$string);
print "String now:\n$string\n";
function show_deleted($matches) {
print "Deleting: ".$matches[0]."\n";
return '';
}
(disclaimer: script tested and seems to work)
Is there any way to print the text between <!-- DeleteBegin --> and <!-- DeleteEnd --> to the screen?
...so I obliged. Figured there must be some other troubleshooting coho75 needed to perform.
[edited by: jatar_k at 6:55 pm (utc) on July 8, 2004]
$string = preg_replace('/(.*<!--\s*DeleteBegin\s*-->)(.*)(<!--\s*DeleteEnd\s*-->.*)/Uis', "$1$3", $string);
If you don't want the comment tags, just move the parentheses in the subpatterns:
$string = preg_replace('/(.*)<!--\s*DeleteBegin\s*-->(.*)<!--\s*DeleteEnd\s*-->(.*)/Uis', "$1$3", $string);
[edited by: coopster at 7:18 pm (utc) on July 8, 2004]
<?php
$filename = "file.html";
####################################
// Read file to change
$fp = fopen($filename, "r") or die("Couldn't open $filename");
$str = fread($fp, 100000);
fclose($fp);
$string = <<<_END
Keep me <!-- DeleteBegin --> but delete me <!-- DeleteEnd --> and keep me too
_END;
print "Original:\n$string\n";
$string = preg_replace_callback('/<!--\s*DeleteBegin\s*-->.*?<!--\s*DeleteEnd\s*-->/','show_deleted',$string);
print "String now:\n$string\n";
function show_deleted($matches) {
print "Deleting: ".$matches[0]."\n";
return '';
}
?>
Thanks to everyone who has given help on this.
coho75
$string = "Keep me <!-- DeleteBegin --> but \n
delete me <!-- DeleteEnd --> and keep me too\n
<table><tr><td>This</td><td>is</td><td>a</td></tr>\n
<tr><td>small</td><td>little</td><td>table!</td></tr></table>\n
Another Keep me <!-- DeleteBegin --> another but delete me <!-- DeleteEnd --> another and keep me too";
print "Original:\n$string\n\n";
print "New:\n";
Next, you run the string through the regular expression. The first set of parentheses matches any text in front of the comment, and will be stored in an incremental variable ($1,$2,$3,etc.) as described in my first post. So, the first opening (or left) parentheses will be placed in $1, the second in $2, etc. These can also be referred to as \\1, \\2, \\3, etc. as the manual states. So by looking at the regex below, we can see that we intend to replace the $string variable with "$1$3", which will be whatever we matched in the first subpattern and whatever was matched in the third subpattern (there is also a special 0 (zero) subpattern that is the entire match).
$string = preg_replace('/(.*)<!--\s*DeleteBegin\s*-->(.*)<!--\s*DeleteEnd\s*-->(.*)/Uis', "$1$3", $string);
print "$string"; Note, as I stated before, the
Uismodifiers [php.net]?
U = Ungreedy
i = case-insenstive
s = include newlines
A little trick to see how subpatterns get captured is to use preg_match and kick out the entire collection using print_r. Don't forget to comment out your preg_replace() function during testing though:
// $string = preg_replace('/(.*)<!--\s*DeleteBegin\s*-->(.*)<!--\s*DeleteEnd\s*-->(.*)/Uis', "$1$3", $string);
preg_match("/(.*<!--\s*DeleteBegin\s*-->)(.*)(<!--\s*DeleteEnd\s*-->.*)/Uis", $string, $matches);
print '<pre>';
print_r($matches);
print '</pre>'; Hope this helps -- coopster
That's why we need to drop the question mark in the original post -- what if you had more than one comment to get rid of? That's what the U and s modifiers are taking care of for us.
Ah - now I see my problem. Though I prefer PHP for web stuff my scripting and regex background is Perl, and the question mark there is a none-greedy modifier. Thus my regex works in Perl (assuming modification for several lines) but not in PHP.
Must read up on PCRE - I've been going through life thinking "Perl Compatible" meant identical.
Apologies for the confusion.