SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    eval() within preg_replace

    Hi Guys,

    I am trying to use eval() within preg_replace, like so but this does not work:

    PHP Code:
    $template = @preg_replace('/\{\$dtsettings\[(.*?)\]\}/e', eval($this->dtsettings["$1"]), $template); 
    Any ideas how to get this to work?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    try
    PHP Code:
    $template preg_replace('/\{\$dtsettings\[(.*?)\]\}/e'"eval($this->dtsettings[.'$1'.])"$template); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm it seems to just exit the script early, no error or nothing - weird

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    `e` modifier already implies eval, no need to use it twice:

    PHP Code:
    $template preg_replace('/\{\$dtsettings\[(.*?)\]\}/e''$this->dtsettings["$1"]'$template); 

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, just tried the following code as a test value but it's not outputting anything! Any ideas?

    PHP Code:
    <? echo "this is a test"?>

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, your array contains php code and you want to eval this code? Try this

    PHP Code:
    $code = array(
        'blah' => '<? echo "hello"?>'
    );

    $template = '1 2 3 {$code[blah]} 4 5';
    $code = eval("return \"$template\";");
    eval("?>$code");

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    So, your array contains php code and you want to eval this code? Try this

    PHP Code:
    $code = array(
        'blah' => '<? echo "hello"?>'
    );

    $template = '1 2 3 {$code[blah]} 4 5';
    $code = eval("return \"$template\";");
    eval("?>$code");
    How can I adapt your idea into my preg_replace? I have tried the following but it does not work:

    PHP Code:
    $template preg_replace('/\{\$dtsettings\[(.*?)\]\}/e', eval("?>$this->dtsettings[$1]"), $template); 

  8. #8
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any ideas?

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, looks like you don't need preg_replace there, just eval() twice. Or, preg_replace and then eval() the new string.

  10. #10
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    well, looks like you don't need preg_replace there, just eval() twice. Or, preg_replace and then eval() the new string.
    Hi,

    Im not quite sure what you mean by the above, would you be able to give me a quick example? Thanks in advance..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •