简体   繁体   中英

echo php code without commenting out php code

Let me rephrase this question. I'm trying to store the contents of an html file into a string. I would then like to have the php code in the html string convert the php to whatever the values I feed it later. I think the string interpolation might work. I might have over complicated it. But I think it would be interesting to still be able to use php tags for certain situations.

I would like to do something like this:

$str = 'some words';
$php = '<p><?= $str; ?></p>';
echo $php;

which would output to the DOM (source):

<p>some words</p>

or simply just on your browser screen

some words

What I get:

<p><!-- <?= $str; ?> --></p>

is this even possible?

I know the code above looks simple but this is just a simple case of the problem I am trying to solve.

<?php

// View
class View {

    private static $paths = [];

    private static function getTemplate($templatePath) {

        if (!array_key_exists($templatePath, self::$paths)) {

            // Filename must exist
            if (!file_exists($templatePath)) {
                die('File Doesn\'t Exist');
            }

            self::$paths[$templatePath] = file_get_contents($templatePath);

        }

        return self::$paths[$templatePath];

    }

    // Fill Template
    public static function fill($vars, $templatePath) {

        // Turn Vars into unique variables
        extract($vars);

        $input = self::getTemplate($templatePath);

        // Get View Output
        ob_start();
        echo $input;
        $output = ob_get_contents();
        ob_end_clean();

        return $output;

    }

    public static function fillWith($templatePath) {

    }

}

Use string interpolation :

echo "<p>$str</p>";

Note the double-quoted syntax ( "..." ). Variables are not substituted in PHP if the string is single-quoted ( '...' ).


As for your updated question

If you obtain the template string from external source, then PHP's string interpolation won't work (it would be a huge security risk if it would).

You may use regular expressions to replace occurrences of specific patterns.

Or use a template engine like Twig . It has more power than you need currently and requires some learning, but it's future-proof in case that you need more complex features sometime.

You may also make your template file a PHP script and include it (instead of file_get_contents ). + Define the variables before the inclusion. Then PHP's usual string interpolation would work. But I do not recommend you to do it this way. It's not readable and introduces a potential security risk.

See also this question .

Thats so simple just use this:

<?php

    $str = "some words";
    echo "<p>$str</p>";
?>

Also some extra information about single and double quotes you finde here: What is the difference between single-quoted and double-quoted strings in PHP?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM