检查页面是否包含<pre> or <code> tags, if yes load files</code></pre>

时间:2012-08-03 04:18:48

标签: php html if-statement

如何使用PHP检查网页是否包含<pre><code>,如果是,则将JavaScript和CSS文件包含在其中。我希望实现这样的目标

if ( condition=true ) {
   echo '<link rel="stylesheet" src="prism.css" />';
   echo '<script src="prism.js"></script>';
}

更新

我想尝试使用代码语法高亮显示。我不希望它被加载到每个页面上,而只是加载包含code的页面。如果您对此荧光笔感兴趣,可以查看他们的code highlighter

4 个答案:

答案 0 :(得分:2)

我认为你把精力集中在错误的地方。确保文件都已正确缓存和缩小,并将它们包含在每个页面上。用户只需下载一次,并将缓存版本用于所有其他页面。

如果你绝对必须,

使用PHP的DOM扩展可以解决这个问题。

<?php

    $html = <<<HTML
<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>

  <code></code>

</body>
</html>
HTML;

    $doc = new DOMDocument();
    $doc->loadHTML($html);

    if ($doc->getElementsByTagName("code")->length != 0) {
        $script_element = $doc->createElement("script");
        $script_element->setAttribute("src", "prism.js");

        $style_element = $doc->createElement("link");
        $style_element->setAttribute("rel", "stylesheet");
        $style_element->setAttribute("href", "prism.css");

        $head_element = $doc->getElementsByTagName("head")->item(0);
        $body_element = $doc->getElementsByTagName("body")->item(0);

        $body_element->appendChild($script_element);
        $head_element->appendChild($style_element);
    }

    $doc->formatOutput = true;
    echo $doc->saveHTML();

这样做的好处是,它会将风格和脚本注入正确的位置。头部的样式和<body>末尾的脚本。

答案 1 :(得分:1)

下载PHP Simple HTML DOM

http://www.coursesweb.net/php-mysql/simple-php-html-dom_pc

将其解压缩并将其放入项目代码文件夹并编写以下代码:

include('php_html_dom/simple_html_dom.php');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

// Write a function with parameter "$elm"
function add_css_js($elm) {
  // if pre or code tag, add css and js
  if ($elm->tag=='pre' || $elm->tag=='code') {
    echo '<link rel="stylesheet" src="prism.css" />';
    echo '<script src="prism.js"></script>';
  }
} 
$html->set_callback('add_css_js');
echo $html;

答案 2 :(得分:0)

输出可以存储在缓冲区中,而不是直接从脚本发送。在发送缓冲区的内容之前,您可以使用回调函数对其进行修改。

<?php
// change the output before sending it out. Replace the first occurrence of <pre>
// with your html.
function callback($buffer)
{
    $str = "<link rel=\"stylesheet\" src=\"prism.css\" />" .
        "<script src=\"prism.js\"></script>";
    if (strpos($buffer, "<pre>") !== false) {
        $buffer = preg_replace('/<pre>/', $str . '<pre>', $buffer,1);
    }
    return $buffer;
}

// turn on your buffer
ob_start("callback");

?>
<html>
<body>
<pre> hello </pre>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
// flush you content here.
ob_end_flush();

?>

输出:

<html>
<head></head>
<body>
    <link rel="stylesheet" src="prism.css">
    <script src="prism.js"></script>
    <pre> hello </pre>
    <p>It's like comparing apples to oranges.</p>
</body>
</html>

参考: http://php.net/manual/en/function.ob-start.php

答案 3 :(得分:-1)

您可以使用preg_match或stristr函数。

例如:

$page = 'Your page content <pre>Something</pre>';

if (stristr($page,'<pre>') || stristr($page,'<code>')) {
   echo '<link rel="stylesheet" src="prism.css" />';
   echo '<script src="prism.js"></script>';
}

如果按“页面”表示页面本身,则使用输出自助餐,如下所示:

ob_start();

echo 'Your page stuff yay!';
echo '<pre>Something</pre';
echo 'End of page';

$page = ob_get_contents();
ob_end_flush();

if (stristr($page,'<pre>') || stristr($page,'<code>')) {
   echo '<link rel="stylesheet" src="prism.css" />';
   echo '<script src="prism.js"></script>';
}