SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
-
May 6, 2008, 08:51 #1
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 Code:$template = preg_replace('/\{\$dtsettings\[(.*?)\]\}/e', '$this->dtsettings["$1"]', $template);
Code:$this->ShowSecurityImage();
Thanks in advance.Last edited by Zaggs; May 6, 2008 at 09:43.
-
May 6, 2008, 12:42 #2
- 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
-
May 6, 2008, 12:47 #3
-
May 6, 2008, 14:45 #4
- 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).
-
May 6, 2008, 15:06 #5
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
-
May 6, 2008, 15:09 #6
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;
}
}
-
May 6, 2008, 15:55 #7
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;
}
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.
-
May 6, 2008, 17:36 #8
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 );
}
}
-
May 7, 2008, 03:34 #9
-
May 7, 2008, 04:09 #10
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
-
May 8, 2008, 05:41 #11
Still can't manage to get this working - anyone have any ideas?
Bookmarks