#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    101
    Rep Power
    16

    Function to convert HTML with PHP in it into pure PHP for eval()


    I wrote this little function so that when I use eval() on HTML with PHP in it (eg: <font color="red"><?php print $name; ?></font>) it will convert the code into pure PHP so eval() works.

    There are a couple of things to note:

    [list=a][*]You CANNOT use the ' character in the PHP code (eg: $val['value']) as it will cause errors - use $val["value"] instead. Using a RegExp will probably fix this but I'm not yet that advanced . You can use ' in HTML code or in print(), echo() in your PHP code.[*]It only works for <?php ?> tags. <? ?> will cause errors.[/list=a]

    PHP Code:
    <?php
    // Convert PHP and HTML code into pure PHP for eval().
    function db2php ($str)     {
            
    $str str_replace("'","\\\'",$str);
            if (
    substr($str,0,5) == "<?php")   {
                    
    $str substr_replace($str,"",0,5);
            }
            else    {
                    
    $str "print '".$str;
            }
            if (
    substr($str,-2) == "?>")   {
                    
    $str substr($str,0,-2);
            }
            else    {
                    
    $str $str."';";
            }
            
    $str str_replace("<?php","';",$str);
            
    $str str_replace("?>","print '",$str);
            return eval(
    $str);
    }
    ?>
    PLEASE NOTE: The line $str = str_replace("'","'",$str); SHOULD READ $str = str_replace("'","\\\'",$str); - The forum is removing my escape characters (VERY ANNOYING ADMINS!!!).

    If anyone likes or uses this script, please tell me about it!
    Last edited by ShepMode; October 1st, 2003 at 07:50 AM.
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,950
    Rep Power
    4554
    How about just using:

    PHP Code:
    eval('?>' $str); 
    ---John Holmes...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    101
    Rep Power
    16
    Originally posted by Sepodati
    How about just using:

    PHP Code:
    eval('?>' $str); 
    ---John Holmes...
    Did you read what I posted? The function works for both HTML and PHP elements TOGETHER. If you eval()d my example above:

    $str = "<font color="red"><?php print $name; ?></font>";
    <?php
    eval('?>' . $str)
    ?>

    it'd PHP equivalent would be:

    <?php
    ?>
    <font color="red"><?php print $name; ?></font>
    ?>

    Which would cause errors...

    By the way have I seen you in comp.lang.php? I recognise your name.
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,950
    Rep Power
    4554
    My bad, this works:

    eval('?>'.$str.'<?php ');


    I'm on the php-general mailing list, and I think all posts to it go into the newsgroup, so yeah, you've probably seen me there.

    ---John Holmes...
    -- Cigars, whiskey and wild, wild women. --
  8. #5
  9. Retired
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Finland
    Posts
    9,115
    Rep Power
    2497
    Not sure what you are getting at, but in your second example
    PHP Code:
    $str = "<font color="red"><?php print $name?></font>";
    that would throw an arror 'cos you haven't excaped your quotes round font.

    PHP Code:
    eval("?".">".$str); 
    works for me

    Br

    Jamie

    Cheers,
    Jamie

    >_ My Music Blog | Losing weight @notsoheavyblog | My Tweets

    __________________

    Let the might of your compassion arise to bring a quick end
    to the flowing stream of the blood and tears .....
    Please hear my anguished words of truth.

    __________________
  10. #6
  11. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,950
    Rep Power
    4554
    So does
    PHP Code:
    eval("?>".$str); 
    , actually.

    ---John Holmes...

IMN logo majestic logo threadwatch logo seochat tools logo