{"id":1702,"date":"2014-11-24T17:34:03","date_gmt":"2014-11-24T16:34:03","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?p=1702"},"modified":"2014-11-24T14:34:58","modified_gmt":"2014-11-24T13:34:58","slug":"php-fragments-in-markdown","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/","title":{"rendered":"PHP fragments in Markdown"},"content":{"rendered":"<p>Ever wanted to use PHP variables and functions in a markdown file and use that file to generate HTML code from the Markdown syntax? Well, it isn&#8217;t too hard&#8230;<\/p>\n<p>Assume you want to parse the following file as Markdown, after executing the PHP fragment:<\/p>\n<pre><code class=\"php\">&lt;?php echo $var; ?&gt; \r\n-------------------\r\n\r\nSome text...\r\n\r\n&gt; Maecenas sed diam eget risus varius blandit sit amet non magna.<\/code><\/pre>\n<p>If you simply use\u00a0<code>file_get_contents($file);<\/code>, it will just show the PHP string, in stead of replacing it with the value of the variable. So we want to grab the executed code in a variable and use that variable to generate HTML.<\/p>\n<p>This is done using <a title=\"PHP Output Buffering Control\" href=\"http:\/\/php.net\/manual\/en\/book.outcontrol.php\" target=\"_blank\">Output Buffering Control<\/a> in PHP. We start an output buffer, include the file, and close the output buffer. Everything that would normally be outputted, is now stored in the output buffer.<\/p>\n<pre><code class=\"php\">&lt;?php\r\n\/\/ start output buffer\r\nob_start();\r\n\r\n\/\/ include the markdown file\r\n\/\/ working with the output buffer allows us to use php code in the md file.\r\ninclude $file;\r\n\r\n\/\/ get the generated content from the output buffer\r\n$md = ob_get_clean();\r\n\r\n\/\/ parse markdown\r\n$html = md_to_html($md);\r\n?&gt;<\/code><\/pre>\n<p><em>Note: If you need newlines in Markdown, you must add a space after the PHP closing tag. I have e.g. added a space after the <code>?&gt;<\/code> on the first line to make the markdown parser work.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wanted to use PHP variables and functions in a markdown file and use that file to generate HTML code from the Markdown syntax? Well, it isn&#8217;t too hard&#8230; Assume you want to parse the following file as Markdown, after executing the PHP fragment: &lt;?php echo $var; ?&gt; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- Some text&#8230; &gt; Maecenas sed diam [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[235,112],"tags":[236,215],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP fragments in Markdown &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP fragments in Markdown &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"Ever wanted to use PHP variables and functions in a markdown file and use that file to generate HTML code from the Markdown syntax? Well, it isn&#8217;t too hard&#8230; Assume you want to parse the following file as Markdown, after executing the PHP fragment: &lt;?php echo $var; ?&gt; ------------------- Some text... &gt; Maecenas sed diam [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2014-11-24T16:34:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-11-24T13:34:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"1 minute\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/\",\"name\":\"PHP fragments in Markdown &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"datePublished\":\"2014-11-24T16:34:03+00:00\",\"dateModified\":\"2014-11-24T13:34:58+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1702"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1702"}],"version-history":[{"count":7,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1702\/revisions"}],"predecessor-version":[{"id":1709,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1702\/revisions\/1709"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=1702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=1702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}