开发者

PHP echo $array[$i] not showing anything

I have the following PHP (Version 5.3.0) code:

$URL = "http://www.example.com/";

I开发者_运维问答F ($URL != "") 
try {
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, $URL);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
    $html = curl_exec ($curl);

    $array = str_split($html);

    for ($i=0;$i<20;$i++) echo $array[$i]," ";

}
 catch (Exception $e) {
      print $e->getMessage();
}

that works as I expect and displays:

< ! D O C T Y P E h t m l P U B L I

Though if I modify the line to:

for ($i=0;$i<20;$i++) echo $array[$i];

then I get nothing displayed. How come nothing is shown in the second case?


Probably because your browser don't like this partial doctype. Try adding header("Content-Type: text/plain") before doing anything.


for ($i=0;$i<20;$i++) echo $array[$i];

Will output a VALID start of an HTML tag, which is interpreted by the browser. Therefore you don't see it.

You can change your entities on the fly using echo htmlentities($array[$i]); or just replace every tag start on your html: $html = str_replace('<','&gt;',$html);


Edit:
As a side note to those who are not familiar with entities:

&gt; is the html entity that represent < just like &lt; represent >. They denote, respectively, greater than and less than and offer an alternate way to print < that otherwise would be interpreted by the browser


Maybe now your browser is trying to render the html output and it just result in a "blank page". Did you try the "view source" option?


You do get the output, but the output is the start of a <DOCTYPE declaration. Why would you expect to see this output visibly? Do "view source" in your browser and you'll see it.


Change to this

for ($i=0;$i<20;$i++) echo htmlspecialchars($array[$i]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜