PHP - 如何用PHP代码替换字符串?

时间:2014-02-04 00:46:58

标签: php string variables str-replace

我是程序员的初学者,如果我的问题很疯狂,我很抱歉,但我很生气:D

我想为我的php文件创建一个模板(或用我的php文件替换我的字符串):

my_string.html

{header}
Some String {foo} Some String {bar} Some String 
{footer}

my_file.php

<div class="header"><img src="logo.png" /></div>
<?php if(isset($foo)) { echo "FOO"; } ?>
<?php if(isset($bar)) { echo "BAR"; } ?>
<div class="footer">mywebsite.com</div>

我可以这样做吗?

<?php $find = array("{header}", "{foo}", "{bar}", "{footer}"); ?>   
<?php $replace = array(
"<div class=\"header\"><img src=\"logo.png"\ /></div>",     
"<?php if(isset(\$foo)) { echo \"FOO\"; } ?>",
"<?php if(isset(\$bar)) { echo \"BAR\"; } ?>",
"<div class=\"footer\">mywebsite.com</div>"); ?>

<?php echo str_replace($find, $replace, implode("<br>", file("my_string.html"))); ?>

my_file.php的结果:

<div class="header"><img src="logo.png" /></div>
Some String FOO Some String BAR Some String 
<div class="footer">mywebsite.com</div>

有没有人能解决我的问题?

3 个答案:

答案 0 :(得分:1)

我在社区维基中复制/粘贴了OP答案,因为它已被附加到问题中。

我将my_file.php文件拆分为某个部分,然后再次重新加入

的header.php

<div class="header"><img src="logo.png" /></div>

foo.php

<?php if(isset($foo)) { echo "FOO"; } ?>

bar.php

<?php if(isset($bar)) { echo "BAR"; } ?>

footer.php

<div class="footer">mywebsite.com</div>

// Var

$header = implode("", file("header.php"));
$foo = implode("", file("foo.php")); 
$bar = implode("", file("bar.php")); 
$footer = implode("", file("footer.php")); 

// New template
$new_template = "new_template.php";

$find = array ("{header}",  "{foo}", "{bar}", "{footer}");
$replace = array ($header, $foo, $bar, $footer);

$contents = str_replace($find, $replace, implode("<br>", file("my_string.html")));
file_put_contents($new_template, $contents);

// Results
include $new_template;

答案 1 :(得分:0)

<?
/**
 * Replace every pattern from the array that match in the subject with 
 * the given value
 * 
 * @$subject The string to search and replace.
 * @$replace_with Array with key Value like
 *      $replace_with = array('{pattern}' => 'Value', 'Foo' => 'BaR');
*/
function template_replace($subject, $replace_with)
{
    // checking
    if(null == $subject) return null;
    if( !is_array($replace_with) || count($replace_with) == 0)
    {
        return $subject;   
    }

    // iterate over the pattern <=> replacement
    foreach($replace_with as $pattern => $replacement)
    {

        $pattern = preg_quote($pattern);

        // Replace all matches to pattern
        $subject = preg_replace(
            "/$pattern/",
            $replacement,
            $subject
        );
    }
    return $subject;
} 

// -----------------


$template = "{header}
Some String {foo} Some String {bar} Some String 
{footer}";

$pattern = array(
    '{header}' => '<div class="header"><img src="logo.png" /></div>',
    '{foo}' => 'Foo',
    '{bar}' => 'Bar',
    '{footer}' => '<div class="footer">mywebsite.com</div>'
    );

$result = template_replace($template, $pattern);

echo $result;

/*
<div class="header"><img src="logo.png" /></div>
Some String Foo Some String Bar Some String 
<div class="footer">mywebsite.com</div>
*/

答案 2 :(得分:0)

如果我做对了,你正在尝试制作自己的模板系统。我会使用正则表达式:

$html         = file_get_contents( 'my_string.html' );

$replacements = array
(
    array( '/{header}/', 'your html code' )
  , // ... the next one
  , // ... and so on
);


foreach( $replacements as $tag )

   $html = preg_replace( preg_quote( $tag[ 0 ] ), $tag[ 1 ], $html );


echo $html;