SitePoint Sponsor

User Tag List

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

    advanced preg_replace help needed

    Hi Guys,

    I am trying to parse html templates which contain nested php variables which should be parsed into functions and then executed. I am using the following code:

    HTML content to be parsed:

    HTML Code:
    <html>{$dtsettings[ShowSecurityImage]}</html>
    PHP parsing code:

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

    Code:
    $this->ShowSecurityImage();
    As you can see the output is not what im looking for - the function is correct but the PHP code is being displayed as opposed to being executed. How can I make the PHP execute?

    Thanks in advance.
    Last edited by Zaggs; May 6, 2008 at 09:43.

  2. #2
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried eval?
    http://www.php.net/eval

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by FrlB View Post
    Have you tried eval?
    http://www.php.net/eval
    Hi - I realise that I need to use eval just not sure how this can be implemented into my code, any ideas?

  4. #4
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure I understand your problem, but eval evaluates strings - so try something like:
    eval("$this->ShowSecurityImage()");
    or
    eval($template)
    But I'm not sure whether this works in classes (I have no PHP here at the moment to verify).

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im using eval within a class and it gives the following error when i use eval($template);

    Code:
    Parse error: syntax error, unexpected '<' in /home/domaintr/public_html/functions/main.class.php(91) : eval()'d code on line 1

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I assume you are using a class so...
    PHP Code:
    class Sometest
    {
        protected function 
    getVaraible $m )
        {
            list( , 
    $prop$key ) = $m;
            if ( 
    property_exists$this$prop ) ) {
                return empty( 
    $key ) ? $this->{$prop} : $this->{$prop}[ $key ];
            }
        }

        public function 
    parse $str )
        {
            
    $str preg_replace_callback'/{$(dtsettings)\[(.*?)]}/', array( $this'getVaraible' ), $str );
            return 
    $str;
        }

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  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 logic_earth View Post
    I assume you are using a class so...
    PHP Code:
    class Sometest
    {
        protected function 
    getVaraible $m )
        {
            list( , 
    $prop$key ) = $m;
            if ( 
    property_exists$this$prop ) ) {
                return empty( 
    $key ) ? $this->{$prop} : $this->{$prop}[ $key ];
            }
        }

        public function 
    parse $str )
        {
            
    $str preg_replace_callback'/{$(dtsettings)\[(.*?)]}/', array( $this'getVaraible' ), $str );
            return 
    $str;
        }



    Hi - Thanks for your reply, but I am trying to get this working on PHP4 +

    I have managed to get my script executing the PHP code using eval, but my problem is now with the output buffer in the parse function. Here is the function I am using;

    PHP Code:
    function parse_template($template) {
            
    // Header fix
            
    ob_start();
            
    $template preg_replace('/\{\$dtsettings\[(.*?)\]\}/e''eval($this->dtsettings["$1"])'$template);
            
    $template ob_get_clean();
            return 
    $template;
        } 
    The problem is that only the output of the ShowSecurityImage() function is being output (the original value of $template is being lost).

    I think it may be due to the ob_get_clean() buffer, but I think this is needed otherwise I received errors? Any idea how to fix this?

    Thanks in advance.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP 4....ehhh

    Don't use eval or the e modifier well you can but it is far less readable and maintainable.

    PHP Code:
    class Sometest
    {
        function 
    getVaraible $m )
        {
            list( , 
    $prop$key ) = $m;
            if ( isset( 
    $prop ) ) {
                return empty( 
    $key ) ? $this->{$prop} : $this->{$prop}[ $key ];
            }
        }

        function 
    parse $str )
        {
            return 
    preg_replace_callback'/{$(dtsettings)\[(.*?)]}/', array( $this'getVaraible' ) , $str );
        }

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP 4....ehhh

    Don't use eval or the e modifier well you can but it is far less readable and maintainable.

    PHP Code:
    class Sometest
    {
        function 
    getVaraible $m )
        {
            list( , 
    $prop$key ) = $m;
            if ( isset( 
    $prop ) ) {
                return empty( 
    $key ) ? $this->{$prop} : $this->{$prop}[ $key ];
            }
        }

        function 
    parse $str )
        {
            return 
    preg_replace_callback'/{$(dtsettings)\[(.*?)]}/', array( $this'getVaraible' ) , $str );
        }

    Hi Logic,

    Thank you for your help, but im now getting this error;

    Code:
    Notice: Undefined property: myClass::${$dtsettings[ShowSecurityImage]} in /home/username/public_html/functions/main.class.php on line 72

  10. #10
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I managed to get it working but as the output of the ShowSecurityImage(); is an image, im receving the following errors;

    Code:
    Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/domaintr/public_html/functions/main.class.php on line 153
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/domaintr/public_html/functions/main.class.php on line 156
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/domaintr/public_html/functions/main.class.php on line 159
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/domaintr/public_html/functions/main.class.php on line 160
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/domaintr/public_html/functions/main.class.php on line 163
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/register.php:80) in /home/domaintr/public_html/functions/main.class.php on line 166
    JFIF>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality C    $.' ",#(7),01444'9=82<.342C  2!!22222222222222222222222222222222222222222222222222<" ĵ}!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz ĵw!1AQaq"2B #3Rbr $4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz ?) @rOvE-ݬxE`^23GZ!x_ hrEmis53:1ʷ\ĉok]DH ( U@FÜdH9#gƍuo*| =KgjEKAړ.>lcoZ?غtFs]%O(s# _k:'8n/˔PeU"01Pj kKicw'c,f@I$ZMm(GApx̴ozaǪjTH΅}q|HN@Z sJI^[)L<>Rp7q;}'Vg u-y$!oXY鎕xV/<8.M晾5+kV] 4+qYHskdFT85RRzO: S>hRi[7[FʗHY'V>}}"2 $2v_r[gMy$A,>P3b hmm-[yFk`rǩ>`+𯁡мqk <ih\h{wO6]kӤ #  yv45d 7_|[^ǣϨG<1p$-3n3Uk\/?SYQm4ϴp y"8k!srAh ZCyRy6 S|D:[iz!ҒHZnB۫gvь35MqqP7. 3>Ckim*jct9DҢy 

  11. #11
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still can't manage to get this working - anyone have any ideas?


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
  •