从存储在mysql数据库中的内容调用php include


Calling a php include from content stored in a mysql database?

我正在用从MySQL数据库检索的内容构建一个网站。url被重新写入一个文件,简化如下:

<html>
 <head>...</head>
 <body>
   <div id="header"></div>
   <?php $content = (html content retrieved from db for the current page); ?>
       <div id="content">
        <?php echo "$content"; ?>
       </div>
   <div id="footer"></div>
 </body>

几乎所有的页面都是一个简单的h1标签和几个p标签,这对简单的文本页面来说很好:

$content = '<h1>Title</h1>
<p>First paragraph of text</p>
<p>Second paragraph of text</p>';

然而,我需要一些页面来在内容中包含PHP文件。例如:

$content = '
<h1>Title</h1>
<p>First paragraph of text</p>
include(imageSlideshow.php)
<p>Second paragraph of text</p>';

PHP include文件中的相同内容将显示在多个页面中,有时需要在同一页面上显示多个include,所以我不想将include文件的代码存储在数据库内容中。是否可以从数据库中检索内容,但以某种方式从该内容中引用PHP include?

我能想到的实现这一目标的唯一方法是1:

在数据库中存储的内容中有一个唯一的刺痛,就像一样

<h1>Title</h1>
<p>First paragraph of text</p>
*KJNJNILKN*imageSlideshow*KJNJNILKN*
<p>Second paragraph of text</p>

然后使用PHP查找对KJNJNILKN的任何此类引用,然后在页面上显示内容之前将其中的内容解析为包含文件,或者2:

我有一个存储页面的表和另一个存储内容的表,如果需要,每个页面都有多个内容行,比如

content table:
id   pageId   contentPosition   contentType   content
1      1             1          html          <h1>Title</h1><p>First para</p>
2      1             2          include       imageSlideshow
3      1             3          html          <p>Second para</p>

然后我可以相应地处理"内容"数据。

如有任何帮助,将不胜感激

您可以使用eval()函数来处理字符串。确保字符串是有效的PHP代码:

$content = '
<h1>Title</h1>
<p>First paragraph of text</p>
include(imageSlideshow.php)
<p>Second paragraph of text</p>';
eval($content);