I'm loading dat开发者_开发知识库a dynamically into a WordPress site:
http://cybart.com/cywp/
- from another site:
http://youngeagles.com/factzone/thisday.asp
To do that, I insert this bit of code into the WordPress page:
<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>
For some reason, this snippet of PHP code wipes out the entire page, leaving it blank and displaying only the data it loads. The effect seems to occur only in Firefox and Chrome; in Safari and IE I can see the website just fine.
I would appreciate someone's expert advice on this.
The code you are fetching contains a call to document.write()
, which will erase all the content if it is called after the page is finished loading:
document.write("\n<P>...<\/P>");
See the notes on the MDC page for document.write for more information.
You might need to parse the code from http://www.youngeagles.com/thisday/absolutecr.asp?z=1 manually, e.g.:
<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>
Note that you'll need to use the s
pattern modifier, as the string you are trying to match has newline characters in it.
精彩评论