Forum Moderators: coopster
For simple reference, let's call our file "example.php". Both the script and the string we're looking for need to be part of "example.php".
example.php
<html><head><title></title></head>
<body><?php if () { echo 'found string';}
else () {echo'could not find string';}<!--stuff-->
<p>stuff</p></body>
</html>
I'm sure this is going to deal with file command(s) and maybe some sort of _self syntax but this is why I'm asking.
John
test.php
<html><head></head><body>
<?php
$fh = fopen('test.php', "rb");
$file = fread($fh, filesize('test.php'));
fclose($fh);
if(strpos($file, "<!--stuff-->")!== false)
{echo "string found";}
else
{echo "string not found";}
?></body></html>
Not really sure how to implement your second suggestion but here was my attempt IoI...
<html><head></head><body>
<?php
if ($foo = preg_grep "<!--stuff-->" $_SERVER['PHP_SELF']`;)
{echo "string found";}?>
</body></html>
Thanks for the suggestions!
John
Also, file_get_contents() is a cleaner way to read a file to a string, unless you intend to alter the file, then use fopen in that case:
$html = file_get_contents($_SERVER['PHP_SELF']);
if(preg_match("/<!--(.*)-->/", $html, $matches)) {
print "<pre>";
print_r($matches);
print "</pre>";
}
The regexp provided by birdman needs a question mark in it:
/<!--(.*?)-->/
... or else the match will be too greedy.
Also, yes, PHP_SELF may not be sufficient (or even work) depending on if the file is included or not.
exec() would work ... but I am personally a fan of backticks, since it is a fast and clean way of returning the output. Either should work though.
Finally, thanks for reminding us about file_get_contents(). That is clearly the most efficient way of getting the file contents :)
Here is a snippet of Matt Wright's guestbook code that does exactly what I want to do but it's written in Perl/CGI)...
# Open Link File to Output
open (GUEST,">$guestbookreal") ¦¦ die "Can't Open $guestbookreal: $!\n";for ($i=0;$i<=$SIZE;$i++) {
$_=$LINES[$i];
if (/<!--begin-->/) {if ($entry_order eq '1') {
print GUEST "<!--begin-->\n";
}
I need to find the exact comment, not any comment (I now use many comments regularly on all my pages). I'm afraid if you can't keep the code simple I'm not going to be able to contribute much also so once something works expect me to ask questions IoI. I'd like to try Birdman's code but I'm thrown off by the *...is this like in DOS dir *.*?
John
$html = file_get_contents($_SERVER['DOCUMENT_ROOT'].$_SERVER['SCRIPT_NAME']);
if (preg_match("/<!--begin-->/", $html, $matches)) {
print '<pre>';
print_r($matches);
print '</pre>';
}
bool ob_start ( [callback output_callback [, int chunk_size [, bool erase]]] )
The "callback" function that you supply can do whatever functions you want to it before outputing it to the user.
There is a slight delay because the user must wait for the fully generated and processed page before it is sent.
Hope this helps.
Barry
I have an idea of whats going on and I have two types of syntax I have a testcase with.
<html><head></head><body>
<?php
$html = file_get_contents('test.php');
if (eregi("<!--begin-->", "$html")) {echo 'eregi match found';}
echo '<br>';
if (preg_match("/<!--begin-->/i", "$html")) {echo 'preg_match match found';}
echo '<br><br>';
echo '<pre>';
echo $html;
echo '</pre>';
?>
</body></html>
It seems that the false positive is happening because when we use file_get_contents PHP is including the PHP code as well! Run this file and you'll see the PHP pass to the client!
Gah! Is there a similar way to file_get_contents that does the same thing BUT has PHP not include itself...or simplified, only counts clientside code?
John
It seems that the false positive is happening because when we use file_get_contents PHP is including the PHP code as well! Run this file and you'll see the PHP pass to the client!
That's because you print $html
<html><head></head><body>
<?php
$html = file_get_contents('test.php');
if (preg_match("/<!--begin-->/i", "$html")) {echo 'preg_match match found';}
else echo 'preg_match match not found';
?>
</body></html>
test1.php
<html><head></head><body>
<?php
$html = file_get_contents('test2.php');
if (eregi("<!--begin-->", "$html")) {echo 'eregi match found';}
echo '<br>';
if (preg_match("/<!--begin-->/i", "$html")) {echo 'preg_match match found';}
?>
</body></html>
test2.php
<html><head></head>
<body><!--begin--></body></html>
Barry, any suggestions on how to implement ob_start in to this to cut it down to a single file?
John
>> when we use file_get_contents PHP is including the PHP code as well!
Yes, it will, unless you read the file in using the URL-syntax as a filename. However, if you do that, and then write the file back out to the file system you are going to overwrite the PHP code in this file.
I think you may have to offer some more specifics on exactly what you are trying to accomplish. At first it sounded like ...