change using str_replace then parse php

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

    change using str_replace then parse php

    ok - please forgive me if i dont make sence - ill try to explain what i want
    to do

    i have an file that i want to change a few srings in it (its a .htm file)

    i use the str_replace funtion to replace the nessasary strings - i then want
    to include the result in the php file that is doing the changes.

    this is what i have got

    MY index.php file is as per below
    ------------------
    <?PHP
    $file=implode(" ",file("main.ht ml"));
    $file=str_repla ce("$stringToFi nd","$NewString ", $file);
    echo $file;
    ?>
    --------------------

    but this doesnt parse any php that is in $file

    i thought a way around this would be to save $file as say file.tmp and then
    use an include statement to include this. then delete file.tmp

    is there any other way around it

    thanks






  • chris

    #2
    Re: change using str_replace then parse php

    i have worked it out - i used the output buffering .




    "chris" <someone@here.c om> wrote in message
    news:41b2cc5c$1 @funnel.arach.n et.au...[color=blue]
    > ok - please forgive me if i dont make sence - ill try to explain what i
    > want to do
    >
    > i have an file that i want to change a few srings in it (its a .htm file)
    >
    > i use the str_replace funtion to replace the nessasary strings - i then
    > want to include the result in the php file that is doing the changes.
    >
    > this is what i have got
    >
    > MY index.php file is as per below
    > ------------------
    > <?PHP
    > $file=implode(" ",file("main.ht ml"));
    > $file=str_repla ce("$stringToFi nd","$NewString ", $file);
    > echo $file;
    > ?>
    > --------------------
    >
    > but this doesnt parse any php that is in $file
    >
    > i thought a way around this would be to save $file as say file.tmp and
    > then use an include statement to include this. then delete file.tmp
    >
    > is there any other way around it
    >
    > thanks
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Hilarion

      #3
      Re: change using str_replace then parse php

      Use eval:

      <?PHP
      $file=implode(" ",file("main.ht ml"));
      $file=str_repla ce("$stringToFi nd","$NewString ", $file);
      eval( '?>' . $file . '<?php' );
      ?>


      Hilarion


      Comment

      • Cameron

        #4
        Re: change using str_replace then parse php

        Hilarion wrote:[color=blue]
        > Use eval:
        >
        > <?PHP
        > $file=implode(" ",file("main.ht ml"));
        > $file=str_repla ce("$stringToFi nd","$NewString ", $file);
        > eval( '?>' . $file . '<?php' );
        > ?>
        >
        >
        > Hilarion
        >
        >[/color]

        N.B. eval should be avoided at all costs it's "expensive"

        Ref: http://uk2.php.net/manual/en/function.eval.php

        ~Cameron

        Comment

        • Hilarion

          #5
          Re: change using str_replace then parse php

          Nothing "should be avoided at all costs" even if it's expensive. It only has
          to be used wisely. (Speed and memory optimization is not the main goal
          of every coding.)

          Hilarion

          PS.: My code should be: eval( '?' . '>' . $file . '<' . '?php ' );


          Comment

          • Janwillem Borleffs

            #6
            Re: change using str_replace then parse php

            Hilarion wrote:[color=blue]
            > Nothing "should be avoided at all costs" even if it's expensive. It
            > only has to be used wisely. (Speed and memory optimization is not the
            > main goal of every coding.)
            >[/color]

            In fact you should read "should only be used as a last resort". Fortunate,
            the OP did find a better solution using output buffering.


            JW



            Comment

            • Cameron

              #7
              Re: change using str_replace then parse php

              Hilarion wrote:[color=blue]
              > Nothing "should be avoided at all costs" even if it's expensive. It only has
              > to be used wisely. (Speed and memory optimization is not the main goal
              > of every coding.)
              >
              > Hilarion
              >
              > PS.: My code should be: eval( '?' . '>' . $file . '<' . '?php ' );
              >
              >[/color]

              Well ok, I meant unless there is no other solution, just worded it
              badly, and one of the comments on the function page contains the quote

              "If eval() is the answer, you're almost certainly asking the
              wrong question. -- Rasmus Lerdorf, BDFL of PHP"

              ~Cameron

              Comment

              • d

                #8
                Re: change using str_replace then parse php

                It's also bad programming, in my experience. I've never seen or even heard
                of a good reason for using it, where some extra coding couldn't do the job
                much faster. I'm not criticising, just expressing my opinion :)

                "Cameron" <cam@foo.bar.in valid> wrote in message
                news:coul8r$dd9 $1$8300dec7@new s.demon.co.uk.. .[color=blue]
                > Hilarion wrote:[color=green]
                >> Use eval:
                >>
                >> <?PHP
                >> $file=implode(" ",file("main.ht ml"));
                >> $file=str_repla ce("$stringToFi nd","$NewString ", $file);
                >> eval( '?>' . $file . '<?php' );
                >> ?>
                >>
                >>
                >> Hilarion[/color]
                >
                > N.B. eval should be avoided at all costs it's "expensive"
                >
                > Ref: http://uk2.php.net/manual/en/function.eval.php
                >
                > ~Cameron[/color]


                Comment

                • d

                  #9
                  Re: change using str_replace then parse php

                  Exactly. Thanks Cameron & Rasmus :)

                  "Cameron" <cam@foo.bar.in valid> wrote in message
                  news:couoi6$128 $1$830fa795@new s.demon.co.uk.. .[color=blue]
                  > Hilarion wrote:[color=green]
                  >> Nothing "should be avoided at all costs" even if it's expensive. It only
                  >> has
                  >> to be used wisely. (Speed and memory optimization is not the main goal
                  >> of every coding.)
                  >>
                  >> Hilarion
                  >>
                  >> PS.: My code should be: eval( '?' . '>' . $file . '<' . '?php ' );[/color]
                  >
                  > Well ok, I meant unless there is no other solution, just worded it badly,
                  > and one of the comments on the function page contains the quote
                  >
                  > "If eval() is the answer, you're almost certainly asking the
                  > wrong question. -- Rasmus Lerdorf, BDFL of PHP"
                  >
                  > ~Cameron[/color]


                  Comment

                  Working...