简体   繁体   English

PHP - 从变量中替换所有{strings}

[英]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. 我想用不同的包含文件替换所有{strings}

For example 例如

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

So the final output of $content to include those files as well. 所以$ content的最终输出也包括那些文件。

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: 您可以使用preg_replace_callback()来标识占位符并将其替换为其他内容:

$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. 上面的代码生成字符串"Hello file1.php. Welcome to file2.php. Status: file3.php" ,这不是你想要的,但它告诉你要走的路。

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: 如果你想把文件file1.php的原始内容而不是{file1} file1.php ,那么使用函数file_get_contents()来获取它:

        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: 如果要include 'file1.php'则需要编写一个小函数来包含该文件并捕获它可能产生的输出:

        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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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