{"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":{"footnotes":""},"categories":[235,112],"tags":[236,215],"class_list":["post-1702","post","type-post","status-publish","format-standard","hentry","category-programming","category-webdevelopment","tag-markdown","tag-php","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP fragments in Markdown &#8211; 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 &#8211; 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 name=\"author\" content=\"Mathias Beke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mathias Beke\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/\"},\"author\":{\"name\":\"Mathias Beke\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\"},\"headline\":\"PHP fragments in Markdown\",\"datePublished\":\"2014-11-24T16:34:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/\"},\"wordCount\":160,\"commentCount\":0,\"keywords\":[\"markdown\",\"php\"],\"articleSection\":[\"programming\",\"Webdevelopment\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/\",\"name\":\"PHP fragments in Markdown &#8211; DenBeke\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\"},\"datePublished\":\"2014-11-24T16:34:03+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/webdevelopment\\\/php-fragments-in-markdown\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP fragments in Markdown\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP fragments in Markdown &#8211; DenBeke","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/","og_locale":"en_US","og_type":"article","og_title":"PHP fragments in Markdown &#8211; DenBeke","og_description":"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;]","og_url":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/","og_site_name":"DenBeke","article_published_time":"2014-11-24T16:34:03+00:00","author":"Mathias Beke","twitter_card":"summary_large_image","twitter_creator":"@MthsBk","twitter_site":"@MthsBk","twitter_misc":{"Written by":"Mathias Beke","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/#article","isPartOf":{"@id":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/"},"author":{"name":"Mathias Beke","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc"},"headline":"PHP fragments in Markdown","datePublished":"2014-11-24T16:34:03+00:00","mainEntityOfPage":{"@id":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/"},"wordCount":160,"commentCount":0,"keywords":["markdown","php"],"articleSection":["programming","Webdevelopment"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/","url":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/","name":"PHP fragments in Markdown &#8211; DenBeke","isPartOf":{"@id":"https:\/\/denbeke.be\/blog\/#website"},"datePublished":"2014-11-24T16:34:03+00:00","author":{"@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc"},"breadcrumb":{"@id":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/denbeke.be\/blog\/webdevelopment\/php-fragments-in-markdown\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/denbeke.be\/blog\/"},{"@type":"ListItem","position":2,"name":"PHP fragments in Markdown"}]},{"@type":"WebSite","@id":"https:\/\/denbeke.be\/blog\/#website","url":"https:\/\/denbeke.be\/blog\/","name":"DenBeke","description":"Mathias Beke","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/denbeke.be\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc","name":"Mathias Beke","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","caption":"Mathias Beke"}}]}},"_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1702","targetHints":{"allow":["GET"]}}],"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}]}}