473,473 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

reading "<?" from a php file

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
12 2240

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
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
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
.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
Don't reinvent the wheel. Use show_source().
http://www.php.net/show_source/

Jul 17 '05 #6
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
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
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
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
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
"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
aa
Thank you both, now I see where my error was.
Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
1
by: Serguei | last post by:
Dear Experts, I try to use a < character as a part of an attribute value in the following way: ......... <ConditionalStatement seqNumber="5" relationalOp="S<"/> ......... If I try to use the...
1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
7
by: Diandian Zhang | last post by:
Does anyone have an idea, how to do it? Thanks in advance!
8
by: Tom | last post by:
Hi I have an aspx page which has javascript to configure xmldata. I added breakpoint to the button method. When I click submit button, it did not go to those breakpoint and show the following...
4
by: Don Wash | last post by:
Hi All! I'm getting the following Error: No DLLs has been compiled yet and nothing in the \bin directory. So it is not the versioning problem or anything like that. And here are the...
2
by: andrew007 | last post by:
I do xml / xslt transformation using asp.net but I found any value (w/xml format) in xml node html-encoded to &lt and &gt format if it's > or < tag. Since I have sub xml data in a parent xml node...
3
by: Steven.Xu | last post by:
hi everybody, i am useing some classes which in System.Xml to deal with xml document. what can i do if the document include "<" or ">"? Thanks.
3
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.