【问题标题】:Calling a php include from content stored in a mysql database?从存储在 mysql 数据库中的内容中调用 php include?
【发布时间】:2014-04-16 20:07:38
【问题描述】:

我正在使用从 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 包含文件中的相同内容将显示在多个页面中,有时需要在同一页面上显示多个包含,因此我不想将代码存储在数据库内容中的包含文件中。是否可以从数据库中检索内容,但以某种方式从该内容中引用 PHP 包含?

我能想到的唯一方法是 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>

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

任何帮助将不胜感激

【问题讨论】:

    标签: php mysql html


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 2010-10-17
      • 2017-10-13
      • 2013-08-14
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多