executing php blocks in a string

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

    #1

    executing php blocks in a string

    From the examples I've seen, I don't think eval() works for this, or
    if it does, I'm not sure how. Here's the setup. I'm storing web page
    content in a database. 95% of the time, all the php takes outside of
    the content area, setting up menus and footers, that sort of thing.
    But some web pages have php code in the middle.


    So I'll have a variable, $content, that looks like this:

    $content = "<p>I'm content</p>\n\n<?php print \"hello world\n"; ?>
    <p>More content</p>";

    How can I get php to execute the code inside the code blocks in the
    variable?

    Thanks,
  • Pedro Graca

    #2
    Re: executing php blocks in a string

    JS wrote:[color=blue]
    > So I'll have a variable, $content, that looks like this:
    >
    > $content = "<p>I'm content</p>\n\n<?php print \"hello world\n"; ?>
    > <p>More content</p>";
    >
    > How can I get php to execute the code inside the code blocks in the
    > variable?[/color]

    This works for me:

    <?php
    $content = "<p>I'm content</p>\n\n<?php print \"hello world\n\";
    ?><p>More content</p>";
    // you were missing a backslash here _______________ _________^__

    // use '?>' to "get out" of PHP mode :)
    eval('?>' . $content);
    ?>

    I also tried the more logical
    eval('?>' . $content . '<?php');
    but that gives me a parse error
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Pedro Graca

      #3
      Re: executing php blocks in a string

      Pedro Graca wrote:[color=blue]
      > This works for me:
      >
      > <?php
      > $content = "<p>I'm content</p>\n\n<?php print \"hello world\n\";
      > ?><p>More content</p>";
      > // you were missing a backslash here _______________ _________^__
      >
      > // use '?>' to "get out" of PHP mode :)[/color]
      Except for the line above

      The ?> ends the comment and the rest gives a parse error

      When I tested it I didn't have the comments in :)
      [color=blue]
      > eval('?>' . $content);
      > ?>[/color]

      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Chung Leong

        #4
        Re: executing php blocks in a string

        eval("?>$conten t<?");

        Uzytkownik "JS" <jd142@hotmail. com> napisal w wiadomosci
        news:b072334c.0 402191430.258fa 1e9@posting.goo gle.com...[color=blue]
        > From the examples I've seen, I don't think eval() works for this, or
        > if it does, I'm not sure how. Here's the setup. I'm storing web page
        > content in a database. 95% of the time, all the php takes outside of
        > the content area, setting up menus and footers, that sort of thing.
        > But some web pages have php code in the middle.
        >
        >
        > So I'll have a variable, $content, that looks like this:
        >
        > $content = "<p>I'm content</p>\n\n<?php print \"hello world\n"; ?>
        > <p>More content</p>";
        >
        > How can I get php to execute the code inside the code blocks in the
        > variable?
        >
        > Thanks,[/color]


        Comment

        • JS

          #5
          Re: executing php blocks in a string

          Thanks, that worked exactly as I hoped once I got my typos out.



          Pedro Graca <hexkid@hotpop. com> wrote in message news:<c13ja4$1e db13$1@ID-203069.news.uni-berlin.de>...[color=blue]
          > Pedro Graca wrote:[color=green]
          > > This works for me:
          > >
          > > <?php
          > > $content = "<p>I'm content</p>\n\n<?php print \"hello world\n\";
          > > ?><p>More content</p>";
          > > // you were missing a backslash here _______________ _________^__
          > >
          > > // use '?>' to "get out" of PHP mode :)[/color]
          > Except for the line above
          >
          > The ?> ends the comment and the rest gives a parse error
          >
          > When I tested it I didn't have the comments in :)
          >[color=green]
          > > eval('?>' . $content);
          > > ?>[/color][/color]

          Comment

          Working...