如果文件是否由 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>©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>