reading "<?" from a php file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aa

    reading "<?" from a php file

    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.ph p","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?





  • Steve

    #2
    Re: reading &quot;&lt;?&quo t; from a php file


    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

    Comment

    • Erwin Moller

      #3
      Re: reading &quot;&lt;?&quo t; from a php file

      aa wrote:
      [color=blue]
      > 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.ph p","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?[/color]

      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:




      Regards,
      Erwin Moller

      Comment

      • Kartic

        #4
        Re: reading &quot;&lt;?&quo t; from a php file

        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

        Comment

        • Michael Fesser

          #5
          Re: reading &quot;&lt;?&quo t; from a php file

          .oO(aa)
          [color=blue]
          >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.[/color]

          htmlspecialchar s() is your friend.


          [color=blue]
          >I tried to skip the line with <? using the following code:[/color]

          Not necessary.
          [color=blue]
          > @ $fp=fopen("a.ph p","r");
          > while (!feof($fp))[/color]

          No error handling? What if the file is not readable?
          [color=blue]
          > $line=fgets($fp );[/color]

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




          HTH
          Micha

          Comment

          • chernyshevsky@hotmail.com

            #6
            Re: reading &quot;&lt;?&quo t; from a php file

            Don't reinvent the wheel. Use show_source().


            Comment

            • aa

              #7
              Re: reading &quot;&lt;?&quo t; from a php file

              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" <netizen@gmx.ne t> wrote in message
              news:58h7u0h896 bi37uhfo7l4uv37 oq6mjab7m@4ax.c om...[color=blue]
              > .oO(aa)
              >[color=green]
              > >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[/color][/color]
              show[color=blue][color=green]
              > >anything, although I can see all the contents in View-Source.[/color]
              >
              > htmlspecialchar s() is your friend.
              >
              > http://www.php.net/htmlspecialchars
              >[color=green]
              > >I tried to skip the line with <? using the following code:[/color]
              >
              > Not necessary.
              >[color=green]
              > > @ $fp=fopen("a.ph p","r");
              > > while (!feof($fp))[/color]
              >
              > No error handling? What if the file is not readable?
              >[color=green]
              > > $line=fgets($fp );[/color]
              >
              > You might also want to have a look at file() or file_get_conten ts().
              >
              > http://www.php.net/file
              > http://www.php.net/file_get_contents
              >
              > HTH
              > Micha[/color]


              Comment

              • Roy W. Andersen

                #8
                Re: reading &quot;&lt;?&quo t; from a php file

                aa wrote:[color=blue]
                > 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.[/color]

                The problem is this line:
                [color=blue]
                > if (!$line=="<?")[/color]

                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

                Comment

                • Daniel Tryba

                  #9
                  Re: reading &quot;&lt;?&quo t; from a php file

                  In comp.lang.php aa <aa@virgin.ne t> wrote:[color=blue]
                  > 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.[/color]

                  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

                  Comment

                  • aa

                    #10
                    Re: reading &quot;&lt;?&quo t; from a php file

                    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" <roy-news@netgoth.or g> wrote in message
                    news:34kl59F4bt rgfU1@individua l.net...[color=blue]
                    > aa wrote:[color=green]
                    > > Thanks for offering other options.
                    > > However I would like to understand what is wrong with the one I am[/color][/color]
                    using?[color=blue][color=green]
                    > > The litaruture does not say that fgets() is not suatable for reading
                    > > characters like "<" which migh happen in any text file.[/color]
                    >
                    > The problem is this line:
                    >[color=green]
                    > > if (!$line=="<?")[/color]
                    >
                    > 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[/color]


                    Comment

                    • Roy W. Andersen

                      #11
                      Re: reading &quot;&lt;?&quo t; from a php file

                      aa wrote:[color=blue]
                      > 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=="<?") ?[/color]

                      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

                      Comment

                      • Richards Noah \(IFR LIT MET\)

                        #12
                        Re: reading &quot;&lt;?&quo t; from a php file

                        "aa" <aa@virgin.ne t> wrote in message
                        news:41e52d6b$0 $55486$ed2619ec @ptn-nntp-reader02.plus.n et...[color=blue]
                        > 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" <roy-news@netgoth.or g> wrote in message
                        > news:34kl59F4bt rgfU1@individua l.net...[color=green]
                        > > aa wrote:[color=darkred]
                        > > > Thanks for offering other options.
                        > > > However I would like to understand what is wrong with the one I am[/color][/color]
                        > using?[color=green][color=darkred]
                        > > > The litaruture does not say that fgets() is not suatable for reading
                        > > > characters like "<" which migh happen in any text file.[/color]
                        > >
                        > > The problem is this line:
                        > >[color=darkred]
                        > > > if (!$line=="<?")[/color]
                        > >
                        > > 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.
                        > >[/color][/color]

                        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


                        Comment

                        • aa

                          #13
                          thaks

                          Thank you both, now I see where my error was.


                          Comment

                          Working...