phpmarkdownparsedown

Can't get ParseDown to parse contents of a file


I am fairly new to PHP. I am trying to get my code to read the content of a Markdown file based on the content of GET attribute "pgid", and then output it. This:

print Parsedown::instance()->text("Testing *Markdown* with **Parsedown**")

results in the output

Testing Markdown with Parsedown

But this:

print (Parsedown::instance()->text(readfile("./".$_GET['pgid'].".md")));

with ?pgid=about and the content of about.md being Testing *Markdown* with **Parsedown**, the output is

Testing *Markdown* with **Parsedown** 39

I am unsure why I can get all of the parts to work separately, but not together.


Solution

  • PHP's readfile() does not return file contents, it outputs them.

    What your code does, is basically this:

    print readfile($filename); // The print() here is implied by readfile itself.
    print (Parsedown::instance()->text(80));
    

    Where 80 is the number of bytes read from the file.

    Instead of readfile(), you'll probably want to use file_get_contents(), which does return the contents of the file.