jbchurchill
jbchurchill

Reputation: 172

What is causing my PHP page to render html tags as text (and what can I do to fix it)?

I have the following PHP code: When it is commented out (as it is now) from just after the comment // ... more stuff in here to the end of that comment block, my page renders html from that 2nd php block as expected as in this screenshot.

If I uncomment that block it renders like the screenshot after the code below which is not what I want (I want the rendered html as in the first screenshot). The only thing not showing in my comment block is a function call that curls a webpage, parses html with DomXNode types of things, and returns an array with 3 elements. How can I get the original rendering of the html back and what am I possibly doing that is ruining that for me? I tried echo instead of print and that makes no difference.

I honestly did search for the answer on here and found lots of pages describing how do do just the opposite of what I want so please be gentle with me. I was surprised that I couldn't find a similar question and I know there has to be an easy answer here. Thanks!

screenshot

<?php 
// ... more stuff in here
  /*
      include("../../includes/curl_fx.php");
      if ($doAppend === "parcel") {
        $lines = explode(PHP_EOL, $Data);
        foreach($lines as $line) {
          if(strpos($line, "http") > 0) {
            $start = stripos(strval($line), "http");
            $fullLength = strlen($line);
            $urlLength = ($fullLength - $start);
            $fullUrl = substr($line, $start, $urlLength);
            $arraySDAT = getSDAT($fullUrl);
            $line .= ", " . $arraySDAT[0] . ", " . $arraySDAT[1] . ", " . $arraySDAT[2] . "\n";
            fwrite($Handle, $line);
          }  
        }
      }
    */
    ?>
    <?php

      if ($DataAdded === true) {
        print "<h2>YourFile.txt</h2>Data has been added.<br />Close this window or tab to return to the web map.<br />";
      } else {
        print "Data may not have been added. Check the file.<br />";
      }
      fclose($Handle);
      print $doAppendAnswer;
      print "<br />";

    ?>

enter image description here

EDIT: Here is the function.

<?php

function getSDAT ($fullUrl="") {

  $ch = curl_init($fullUrl);
  if (! $ch) {
    die( "Cannot allocate a new PHP-CURL handle" );
  }
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $data = curl_exec($ch);
  header("Content-type: text");
  curl_close($ch);

  libxml_use_internal_errors(true);
  libxml_clear_errors();

  $doc = DOMDocument::loadHTML($data);
  $xpath = new DOMXPath($doc);

  $ownName1query = '//table/tr/td/span[@id="MainContent_MainContent_cphMainContentArea_ucSearchType_wzrdRealPropertySearch_ucDetailsSearch_dlstDetaisSearch_lblOwnerName_0"][@class="text"]';
  $ownName2query = '//table/tr/td/span[@id="MainContent_MainContent_cphMainContentArea_ucSearchType_wzrdRealPropertySearch_ucDetailsSearch_dlstDetaisSearch_lblOwnerName2_0"][@class="text"]';
  $ownAddrquery = '//table/tr/td/span[@id="MainContent_MainContent_cphMainContentArea_ucSearchType_wzrdRealPropertySearch_ucDetailsSearch_dlstDetaisSearch_lblMailingAddress_0"][@class="text"]';

  $entries = $xpath->query($ownName1query);
  foreach($entries as $entry) {
    $ownname1 = $entry->nodeValue;
  }

  $entries = $xpath->query($ownName2query);
  foreach($entries as $entry) {
    $ownname2 = $entry->nodeValue;
  }

  $entries = $xpath->query($ownAddrquery);
  $pattern = '#<br\s*/?>#i';
  $replacement = ", ";
  $i=0;
  foreach($entries as $entry) {
    $ownAddr = $entry->nodeValue;
    if(!$entry->childNodes == 0) {
      $ownAddr = $doc->saveHTML($entry);      
    }
    $ownAddr2 = preg_replace($pattern, $replacement, $ownAddr, 15, $count); // replace <br/> with a comma
    $ownAddr3 = strip_tags($ownAddr2);
  }

  return array($ownname1, $ownname2, $ownAddr3);
}

Upvotes: 2

Views: 2843

Answers (2)

RJBaytos
RJBaytos

Reputation: 71

As mentioned, it's the header which is causing you a problem. You see the header decides the type of content that the current document should have or how the current document should behave - an information that is usually found in the < head >...< /head > part of an HTML. You can use it for declaring the content type, controlling the cache, redirecting, and etc.

When you use header("Content-type: text"), you are deciding that the content of the current document "yourdocument.php" would be a text instead of the default which is HTML.

header("Content-type: text/html");
echo "<html>This  would make mypage.php behave as an HTML</html>";
// This is usually unnecessary since text/html is already the default header

header("Content-type: text/javascript");
echo "this would make mypage.php behave as a javascript";

header("Content-type: text/css");
echo "this would make mypage.php behave as a CSS";

header('Content-type: image/jpeg');
readfile("source/to/my/file.jpg");
// this would make mypage.php display file.jpg and act as a jpg

header('Content-type: image/png');
readfile("source/to/my/file.png");
// this would make mypage.php display file.png and act as a png

header('Content-type: image/gif');
readfile("source/to/my/file.gif");
// this would make mypage.php display file.gif and act as a gif

header('Content-type: image/x-icon');
readfile("source/to/my/file.ico");
// this would make mypage.php display file.ico and act as an icon

header('Content-type: image/x-win-bitmap');
readfile("source/to/my/file.cur");
// this would make mypage.php display file.cur and act as a cursor

Upvotes: 1

Tea Curran
Tea Curran

Reputation: 2983

Your problem is:

header("Content-type: text");

Just remove that. Why is it there?

Upvotes: 6

Related Questions