PHP-如何用PHP代码替换字符串?
我是一个程序员新手,如果我的问题是疯狂的话,我很抱歉,但我对它非常着迷:D 我想为我的php文件创建一个模板(或者用我的php文件替换我的字符串): my_string.htmlPHP-如何用PHP代码替换字符串?,php,string,variables,str-replace,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(i
{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>
一些字符串FOO一些字符串条一些字符串
我的网站
有人能解决我的问题吗?
自从OP答案被添加到问题后,我将其复制/粘贴到了一个社区wiki中。
<?
/**
* 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>
*/
我将我的_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;
mywebsite.com
//变量
$header=内爆(“,文件(“header.php”);
$foo=内爆(“,文件(“foo.php”);
$bar=内爆(“,文件(“bar.php”);
$footer=内爆(“,文件(“footer.php”);
//新模板
$new\u template=“new\u template.php”;
$find=array(“{header}”、“{foo}”、“{bar}”、“{footer}”);
$replace=array($header、$foo、$bar、$footer);
$contents=str_replace($find,$replace,内爆(
),文件(“my_string.html”);
文件内容($new\u模板,$contents);
//结果
包括$new_模板;
如果我没弄错,您正在尝试创建自己的模板系统。我会使用正则表达式:
$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;
您是否正在尝试创建自己的模板系统?这个问题不是很清楚。@nus是的,没错。。我想为我的_file.php创建一个模板。实际上,在尝试了另外20个验证码后,我敢肯定他们甚至不看答案是否正确。我肯定没有把所有的验证码都弄对,因为它们绝对不可读,但我肯定有一些是对的。我不想再帮助stackexchange上的人了。byeHi@nus谢谢你的回答,我终于解决了。。我将我的php文件拆分成一些部分文件,并再次将其连接成一个文件..您好@AjieKurniyawan,欢迎来到Stack Overflow。在这里,我们更喜欢将问题和答案分开。因此,我将你的答案分为一个维基答案(我不会从中获得声誉),所以请随意接受它。最好的办法是正确地回答以下表格中的问题,并接受自己的答案,以便其他人看到问题已经得到了回答。此php代码
和
只是一些示例,我的php代码更复杂。。也许它无法处理这样的数组:$pattern=array('{foo}'=>'foo','{bar}'=>'bar',…)代码>如果您的代码很长,请将它们放入文件中,然后使用文件获取内容
将它们放入数组中,或者查看现有的模板库,我会说(有很多,为什么要发明热水?。@Wuiso,您的代码中有一个错误。模式不是有效的正则表达式,因为需要转义{和}。您可以使用preg\u quote
自动执行此操作。