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

PHP-替换变量中的所有{strings},php,str-replace,Php,Str Replace,我有一个变量: $content = "Hello {file1}. Welcome to {file2}. Status: {file3}"; 我想用不同的include文件替换所有{strings} 比如说 {file1} = file1.php {file2} = file2.php {file3} = file3.php 因此,$content的最终输出也包括这些文件 我该怎么做呢?这里您的起始delimeter应该是“{”和结束delimeter“}”,参数是在数组中收集的文件名变

我有一个变量:

$content = "Hello {file1}. Welcome to {file2}. Status: {file3}";
我想用不同的include文件替换所有
{strings}

比如说

{file1} = file1.php
{file2} = file2.php
{file3} = file3.php
因此,$content的最终输出也包括这些文件


我该怎么做呢?

这里您的起始delimeter应该是“{”和结束delimeter“}”,参数是在数组中收集的文件名变量

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

$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
);
上面的代码生成字符串
“Hello file1.php.Welcome to file2.php.Status:file3.php”
,这不是您想要的,但它为您指明了方向

如果要将文件
file1.php
的原始内容而不是
{file1}
a.s.o.放入,请使用函数获取:

        if (isset($replacements[$item])) {
            // replace with the raw content of the file
            $item = file_get_contents($item);
        }
如果需要,则需要编写一个小函数来包含该文件并捕获它可能产生的输出:

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

只需使用变量并将变量放入字符串中?只需脱离主题,但如果这实际上是您的变量,我假设您的字符串非常短,并且我不认为将小字符串存储在不同的文件中是最好的。我忘了提到我正在使用CKEditor,它会注释出php,这就是为什么我想到添加{strings}然后用str_replace替换为include文件?这是一条非常重要的信息,您已经输入并更改了所有内容。现在这些都没有意义了。您希望文件名在那里还是文件名的内容??