1

I have the following PHP (Version 5.3.0) code:

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

IF ($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?

Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136
  • 1
    Also, if you're looking to just breakdown a string, try using `$html{$i}` (this brings it more traditional to C-Style strings and can reference individual string characters). – Brad Christie Jan 19 '11 at 15:27

5 Answers5

3

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

sloonz
  • 254
  • 2
  • 4
3
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

acm
  • 6,541
  • 3
  • 39
  • 44
  • Thanks for the input. I am trying to extract a list of all the words in the text of the html, and get rid of all tags and code of any sort. So this was my first step to handle the html character by character. What is '>' ? – Timothée HENRY Jan 19 '11 at 15:49
1

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?

Francesco Laurita
  • 23,434
  • 8
  • 55
  • 63
1

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Change to this

for ($i=0;$i<20;$i++) echo htmlspecialchars($array[$i]);
Matt Asbury
  • 5,644
  • 2
  • 21
  • 29