new DomDocument()不适用于getElementsByTagName('section');

时间:2013-04-23 18:50:44

标签: php ajax jquery domdocument

如果文件是由Ajax调用的,我想有不同的输出。 为此,我有这个代码巫婆工作正常:

/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $content = get_content();
    die($content);
}

我过滤竞争的功能是:

function get_content(){
    //$file = file_get_contents('index.html'); //works better but i get only errors
    $dom = new DomDocument();
    $dom->loadHTML($_SERVER['REQUEST_URI']); //or $file
    $section= $dom->getElementsByTagName('section');
    return $section->item(0)->nodeValue;
}

我的输出始终为空。 这有什么不对吗?

!!!我不是在寻找DOMDocument错误处理!!!!

我现在所做的是:

/* decide what the content should be up here .... */

/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    /* if ajax,  show only content of the section tag */
    $file = file_get_contents($_SERVER['SCRIPT_FILENAME']);
    $section = preg_replace("/.*<section[^>]*>|<\/section>.*/si", "", $file);
    die($section);
}

/* not ajax, show page.... */

它似乎有效。我不知道这是最好的解决方案,因为在这个论坛的某个地方,我读到了用DomDocument()更好地认识到它。 欢迎任何建议?

这是html文件:

<!doctype html>
<!-- Determine browser JS free -->
<!-- HTML5 Mobile Boilerplate -->
<!--[if IEMobile 7]><html class="no-js iem7"><![endif]-->
<!--[if (gt IEMobile 7)|!(IEMobile)]><!--><html class="no-js" lang="en"><!--<![endif]-->

<!-- HTML5 Boilerplate -->
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if (IE 7)&!(IEMobile)]><html class="no-js lt-ie9 lt-ie8" lang="en"><![endif]-->
<!--[if (IE 8)&!(IEMobile)]><html class="no-js lt-ie9" lang="en"><![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"><!--<![endif]-->


<head>
                    <meta charset="utf-8">
            <title>title</title>


            <link rel="stylesheet" type="text/css" href="../css/style.css">
            <link rel="shortcut icon" href="img/base/favicon.html" />
            <link rel="apple-touch-icon-precomposed" href="img/base/apple-touch-icon-57x57-precomposed.html" />
            <link rel="apple-touch-icon-precomposed" sizes="72x72" href="img/base/apple-touch-icon-72x72-precomposed.html" />
            <link rel="apple-touch-icon-precomposed" sizes="114x114" href="img/base/apple-touch-icon-114x114-precomposed.html" />
            <script src="/js/libs/modernizr.custom.js"></script>
            <!--[if (IE 7)]>
                <link rel="stylesheet" href="/css/fallback/icons-ie7.css">
            <![endif]-->

            </head>

    <body >


        <section role="main">

    <header class="hero-banner">
        <img class="resize" src="/img/base/spacer-hero.png" rel="/img/designers/banners/cloe-banner3.jpg" alt="Cloe" />
        <noscript><img src="/img/designers/banners/cloe-banner3.jpg" alt="Cloe" /></noscript>
        <hgroup>
            <h1>Claudia</h1>
            <h2 class="text-hl">Designerin</h2>
        </hgroup>
    </header>


        </section>



        <footer role="contentinfo">


            <p><small>&copy;2013 joe</small></p>
            <p><small>Our website uses delicious cookies to improve your experience. We'll assume you're ok with that.</small></p>


        </footer>


        <!-- jQuery from Google's CDN with local fallback -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="/js/libs/jquery-1.8.3.min.js"><\/script>')</script>

        <script src="/js/libs/wfl.js"></script>

        <!-- Initialises scripts from plugins.js and helper.js -->
        <script src="/js/script.js"></script>

        <!-- Google Goggles -->


    </body>

</html>

1 个答案:

答案 0 :(得分:0)

您可以通过将内部错误设置为true来转换错误:

$doc = new DOMDocument;

$before = libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors($before);

然后您将不再记录错误。在您的情况下,您很可能已启用显示错误,这可能适合开发,但在生产服务器上,请切换到错误日志记录,否则您的网站会向外部显示太多数据。

参见: