By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,378 Members | 3,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,378 IT Pros & Developers. It's quick & easy.

reading "<?" from a php file

P: n/a
aa
I am trying to read a.php file from another PHP file using fgets() and
display its contents. As a.php starts with <? the browser IE6 does not show
anything, although I can see all the contents in View-Source.

I tried to skip the line with <? using the following code:

@ $fp=fopen("a.php","r");
while (!feof($fp))
{
$line=fgets($fp);
if (!$line=="<?")
{
echo $line;
}
else
{
echo "w";
}
However it keeps outputting "w" for every line of a.php.
This does not change even is I reverse the condition to if (!$line=="<?")
as if whatever is returened by fgets($fp) cannot be compared to "<?".
I tried to add the end of the line to the pattern (!$line=="<?\n") or
($line=="<?\n") - same result.

What is that I am doing wrong?

Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

Your first line, which opens the file, suppresses any error message and
you don't test for any errors.

I bet you this $ sign that if you remove the '@' you will see an error
message. Tell us what the error message is and we can help.

---
Steve

Jul 17 '05 #2

P: n/a
aa wrote:
I am trying to read a.php file from another PHP file using fgets() and
display its contents. As a.php starts with <? the browser IE6 does not
show anything, although I can see all the contents in View-Source.

I tried to skip the line with <? using the following code:

@ $fp=fopen("a.php","r");
while (!feof($fp))
{
$line=fgets($fp);
if (!$line=="<?")
{
echo $line;
}
else
{
echo "w";
}
However it keeps outputting "w" for every line of a.php.
This does not change even is I reverse the condition to if (!$line=="<?")
as if whatever is returened by fgets($fp) cannot be compared to "<?".
I tried to add the end of the line to the pattern (!$line=="<?\n") or
($line=="<?\n") - same result.

What is that I am doing wrong?


Hi,

I think you better use htmlentities. That function was written to display
'difficult' pieces of text into a HMTL page.
I suspect that if you just drop your <? piece and use html_entities, you get
what you are looking for.

Check this:

http://nl3.php.net/manual/en/function.htmlentities.php
Regards,
Erwin Moller
Jul 17 '05 #3

P: n/a
How about using
<?php
print htmlentities( join('', file('a.php')) );
?>

This reads the file and escapes all the HTML entities in your file and
makes the output browser-ready.

For a complete script (and as well as concrete idea), please take a
look at http://www.php.net/source.php?url=/source.php

(Source of PHP.net pages can be seen by click the view source link at
the bottom of each page... above link is the source of source.php that
does the job)

Thanks,
--Kartic

Jul 17 '05 #4

P: n/a
.oO(aa)
I am trying to read a.php file from another PHP file using fgets() and
display its contents. As a.php starts with <? the browser IE6 does not show
anything, although I can see all the contents in View-Source.
htmlspecialchars() is your friend.

http://www.php.net/htmlspecialchars
I tried to skip the line with <? using the following code:
Not necessary.
@ $fp=fopen("a.php","r");
while (!feof($fp))
No error handling? What if the file is not readable?
$line=fgets($fp);


You might also want to have a look at file() or file_get_contents().

http://www.php.net/file
http://www.php.net/file_get_contents

HTH
Micha
Jul 17 '05 #5

P: n/a
Don't reinvent the wheel. Use show_source().
http://www.php.net/show_source/

Jul 17 '05 #6

P: n/a
aa
Thanks for offering other options.
However I would like to understand what is wrong with the one I am using?
The litaruture does not say that fgets() is not suatable for reading
characters like "<" which migh happen in any text file.

"Michael Fesser" <ne*****@gmx.net> wrote in message
news:58********************************@4ax.com...
.oO(aa)
I am trying to read a.php file from another PHP file using fgets() and
display its contents. As a.php starts with <? the browser IE6 does not showanything, although I can see all the contents in View-Source.


htmlspecialchars() is your friend.

http://www.php.net/htmlspecialchars
I tried to skip the line with <? using the following code:


Not necessary.
@ $fp=fopen("a.php","r");
while (!feof($fp))


No error handling? What if the file is not readable?
$line=fgets($fp);


You might also want to have a look at file() or file_get_contents().

http://www.php.net/file
http://www.php.net/file_get_contents

HTH
Micha

Jul 17 '05 #7

P: n/a
aa wrote:
Thanks for offering other options.
However I would like to understand what is wrong with the one I am using?
The litaruture does not say that fgets() is not suatable for reading
characters like "<" which migh happen in any text file.
The problem is this line:
if (!$line=="<?")


You're basically saying "if $line is zero equals <?", which always fails
since $line isn't zero (nor would if equal <? if it ever was zero).

Try this instead: if ($line != "<?")

That one says "if $line isn't <?" which is exactly what you're trying to
say.
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #8

P: n/a
In comp.lang.php aa <aa@virgin.net> wrote:
Thanks for offering other options.
However I would like to understand what is wrong with the one I am using?
The litaruture does not say that fgets() is not suatable for reading
characters like "<" which migh happen in any text file.


Check thr fgets function: on a newline (which is included in the return
value)

Seeing that you post TOFU style with broken quotes, I guess you are
running on the windows platform where a newline is not \n. So either
trim the read line or (better) use strpos.

FUP to comp.lang.php
Jul 17 '05 #9

P: n/a
aa
Thanks, Roy, I felt I was missing something fundamental in PHP syntax.
Will you please explain how
if(!$line=="<?")
is interpreted by PHP as
"if $line is zero equals <?" ? I.e. if(!$line= (0=="<?"))

Where zero is implied in (!$line=="<?") ?

"Roy W. Andersen" <ro******@netgoth.org> wrote in message
news:34*************@individual.net...
aa wrote:
Thanks for offering other options.
However I would like to understand what is wrong with the one I am using? The litaruture does not say that fgets() is not suatable for reading
characters like "<" which migh happen in any text file.


The problem is this line:
> if (!$line=="<?")


You're basically saying "if $line is zero equals <?", which always fails
since $line isn't zero (nor would if equal <? if it ever was zero).

Try this instead: if ($line != "<?")

That one says "if $line isn't <?" which is exactly what you're trying to
say.
Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama

Jul 17 '05 #10

P: n/a
aa wrote:
Thanks, Roy, I felt I was missing something fundamental in PHP syntax.
Will you please explain how
if(!$line=="<?")
is interpreted by PHP as
"if $line is zero equals <?" ? I.e. if(!$line= (0=="<?"))

Where zero is implied in (!$line=="<?") ?


Putting an exclamation-mark in front of a variable like that will test
the value of the variable. I said zero, but I guess "false" would be
more appropriate. I didn't mean the number 0, but zero as in nothing,
none, null, nada - call it what you like, just be aware that PHP will
probably want to call it false ;)

if (!$test) {
do_stuff();
}

In this case, do_stuff() will be called if $test is false, meaning if
$test is not set, if it contains the value 0, if it's set to null/false,
or if it contains an empty string (e.g. '', or "" if you prefer).

if (!$test == "some string") {
do_stuff();
}

In this case, do_stuff() will be called if $test is false and equal to
"some string", which by it's very definition isn't false - "some string"
can only be false if it's "", in which case it's no longer "some
string", and there it goes around and around until we get dizzy and give
up - PHP is smarter than us, so it just says "this won't work" and moves
on. The statement will never evaluate to true, and do_stuff() will never
happen.

Basically, just keep it simple. Ask the question in plain english, and
then translate it into PHP code. If you want to ask if $a equals $b,
then ask if $a == $b. If you want to ask if $a doesn't equal $b, then
ask if $a != $b. If you want to ask if $a is false, then you can either
ask if !$a, or if $a == false.

Roy W. Andersen
--
ra at broadpark dot no / http://roy.netgoth.org/

"Hey! What kind of party is this? There's no booze
and only one hooker!" - Bender, Futurama
Jul 17 '05 #11

P: n/a
"aa" <aa@virgin.net> wrote in message
news:41***********************@ptn-nntp-reader02.plus.net...
Thanks, Roy, I felt I was missing something fundamental in PHP syntax.
Will you please explain how
if(!$line=="<?")
is interpreted by PHP as
"if $line is zero equals <?" ? I.e. if(!$line= (0=="<?"))

Where zero is implied in (!$line=="<?") ?

"Roy W. Andersen" <ro******@netgoth.org> wrote in message
news:34*************@individual.net...
aa wrote:
Thanks for offering other options.
However I would like to understand what is wrong with the one I am using? The litaruture does not say that fgets() is not suatable for reading
characters like "<" which migh happen in any text file.


The problem is this line:
> if (!$line=="<?")


You're basically saying "if $line is zero equals <?", which always fails
since $line isn't zero (nor would if equal <? if it ever was zero).

Try this instead: if ($line != "<?")

That one says "if $line isn't <?" which is exactly what you're trying to
say.


He didn't really state that clearly. He meant "is equal to" rather than
"equals".

This:
if(!$line == "<?")

Equates to:
if( !($line) == "<?")

As the '!' has higher precedence than the '=='.

So, either use:
if($line != "<?")

Or use:
if(! ($line == "<?"))

(the former is better).

-Noah
Jul 17 '05 #12

P: n/a
aa
Thank you both, now I see where my error was.
Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.