我正在将数据动态加载到WordPress网站中:
http://youngeagles.com/factzone/thisday.asp
为此,我将这段代码插入到WordPress页面中:
<div id="this_day_in_history">
<h3>This Day in Aviation History</h3>
<?php
$contents=file_get_contents('http://www.youngeagles.com/thisday/absolutecr.asp?z=1');
$convertedcontents=iconv("ISO-8859-1", "UTF-8//IGNORE//TRANSLIT", $contents);
echo "<script type=\"text/javascript\">".$convertedcontents."</script>";
?>
</div>
出于某种原因,这段PHP代码会清除整个页面,将其留空并只显示它加载的数据。效果似乎只出现在Firefox和Chrome中;在Safari和IE中我可以看到网站就好了。
我很感激某人的专家建议。
答案 0 :(得分:3)
您提取的代码包含对document.write()
的调用,如果在页面加载完成后调用它,将删除所有内容:
document.write("\n<P>...<\/P>");
有关详细信息,请参阅MDC page for document.write上的说明。
您可能需要手动解析http://www.youngeagles.com/thisday/absolutecr.asp?z=1中的代码,例如:
<div id="this_day_in_history">
<h3>This Day in Aviation History</h3>
<?php
$contents=file_get_contents('http://www.youngeagles.com/thisday/absolutecr.asp?z=1');
$convertedcontents=iconv("ISO-8859-1", "UTF-8//IGNORE//TRANSLIT", $contents);
if( preg_match('#^document\.write\("(.+)"\);$#s', $convertedcontents, $matches) )
{
echo stripslashes(str_replace('\\n', '', $matches[1]));
}
else
{
// TODO Format of $convertedcontents has changed. Log for developer review.
}
?>
</div>
请注意,您需要使用s
pattern modifier,因为您要匹配的字符串中包含换行符。