1
4

4 回答 4

2

I think you're focusing your effort in the wrong place. Make sure the files are both properly cached and minified, and include them on every page. The user will only download them once, and use the cached version for all further pages.

If you absolutely must,

Using PHP's DOM extension can solve this problem.

<?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();

The upside of this, is that it will inject the style and script in the correct places. Style at the head, and script at the end of <body>.

于 2012-08-03T07:35:59.680 回答
1

Download PHP Simple HTML DOM from

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

extract it and put it into your project code folder and write following code :

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;
于 2012-08-03T07:27:00.117 回答
0

Output can be stored in a buffer instead of sending it directly from the script. Before sending out the content of the buffer you can modify it using a callback function.

<?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();

?>

Output:

<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>

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

于 2012-08-03T05:12:07.313 回答
-1

You could either use a preg_match or stristr functions.

For example:

$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>';
}

If by "Page" you mean the page itself then use the output buffet like this:

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>';
}
于 2012-08-03T04:24:03.297 回答