简体   繁体   中英

PHP - Replace all {strings} from variable

I have this variable:

$content = "Hello {file1}. Welcome to {file2}. Status: {file3}";

I want to replace all {strings} with different include files.

For example

{file1} = file1.php
{file2} = file2.php
{file3} = file3.php

So the final output of $content to include those files as well.

How can i do it?

Here your starting delimeter should be "{" and ending delimeter "}" and the parameters are your file name variables collected in an array.

while (true) {
            //Find starting delimiter
            $paramStart = strpos($this->_templateText, $this->_delimiters[0], $index);

            //No more starting delimiters. Copy rest of the message and return
            if ($paramStart === false) {
                $message .= substr($this->_templateText, $index);
                break;
            }

            //Copy portion up to the starting delimiter to message
            $message .= substr($this->_templateText, $index, $paramStart-$index);

            //Find ending delimiter
            $paramEnd = strpos($this->_templateText, $this->_delimiters[1], $paramStart + $startDelimiterLength);

            //No matching ending delimiter
            if ($paramEnd === false) {
                break;
            }

            //Get the portion between delimiters (i.e. parameter name)
            $paramName = substr(
                $this->_templateText,
                $paramStart + $startDelimiterLength,
                $paramEnd - ($paramStart + $startDelimiterLength)
            );

            //Insert parameter into message
            if (isset($parameters[$paramName])) {
                $message .= $parameters[$paramName];
            }

            //Continue from where we left off
            $index = $paramEnd + $endDelimiterLength;
        }

You can use preg_replace_callback() to identify the placeholders and replace them with something else:

$content = "Hello {file1}. Welcome to {file2}. Status: {file3}";
$replacements = array(
    '{file1}' => 'file1.php',
    '{file2}' => 'file2.php',
    '{file3}' => 'file3.php',
);

$output = preg_replace_callback(
    '/\{.*?\}/',
    function (array $matches) use ($replacements) {
        $item = $matches[0];
        if (isset($replacements[$item])) {
            // simple replacement: replace the key in $replacements with the value
            $item = $replacements[$item];
        }
        return $item;
    },
    $content
);

The code above produces the string "Hello file1.php. Welcome to file2.php. Status: file3.php" which is not what you want but it shows you the way to go.

If you want to put the raw content of file file1.php instead of {file1} aso then use function file_get_contents() to get it:

        if (isset($replacements[$item])) {
            // replace with the raw content of the file
            $item = file_get_contents($item);
        }

If you want to include 'file1.php' then you need to write a small function to include the file and capture the output it might produce:

        if (isset($replacements[$item])) {
            // replace with the content produced by the included file
            $item = includeFile($item);
        }

// ... somewhere after the rest of the code...
function includeFile($file)
{
    ob_start();
    include($file);
    return ob_get_clean();
}

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