简体   繁体   English

替换包含 php 的页面的文本词

[英]Replace a text word for a php included page

I'd like to replace in text [[banner]] to an ad banner.我想将文本[[banner]]替换为广告横幅。

For example:例如:

some text...
[[banner]]
some text...

I'd like php to find in my text all [[banner]] words and include a php page in its place, so the result would be the same as if I did:我希望 php 在我的文本中找到所有[[banner]]单词并在其位置包含一个 php 页面,因此结果将与我所做的相同:

some text...
<?php include ("banner.php"); ?>
some text...

any ideas if it is possible?如果可能的话,有什么想法吗?

Using preg_replace here should be viable:在这里使用preg_replace应该是可行的:

$input = "some text...\n[[banner]]\nsome text...";
$output = preg_replace("/\[\[(.*?)\]\]/", "<?php include (\"$1.php\"); ?>", $input);
echo $output;

This prints:这打印:

some text...
<?php include ("banner.php"); ?>
some text...

Note that I assume that the name of the PHP script file is whatever text be contained within [[banner]] .请注意,我假设 PHP 脚本文件的名称是[[banner]]包含的任何文本。 If not, then state the logic here.如果没有,请在此处说明逻辑。

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

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