{"id":6320,"date":"2014-04-15T19:46:09","date_gmt":"2014-04-15T19:46:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/php-str_replace-template-with-placeholders-collection-of-common-programming-errors\/"},"modified":"2014-04-15T19:46:09","modified_gmt":"2014-04-15T19:46:09","slug":"php-str_replace-template-with-placeholders-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/php-str_replace-template-with-placeholders-collection-of-common-programming-errors\/","title":{"rendered":"php str_replace template with placeholders-Collection of common programming errors"},"content":{"rendered":"<p>i think it won&#8217;t work with str_replace easily so i&#8217;m going to use preg_replace<\/p>\n<pre><code>$data = array('title'=&gt;'some title', 'date'=&gt;1350498600, 'story'=&gt;'Some story');\n$template = \"#title#, <br \/>#date(d)#<br \/> #date(m)#<br \/>#date(Y)#<br \/> #story#\"; \n$result = preg_replace_callback('\/#(\\w+)(?:\\\\((.*?)\\\\))?#\/', function ($match) use($data) {\n    $value = isset($data[$match[1]]) ? $data[$match[1]] : null;\n\n    if (!$value) {\n        \/\/ undefined variable in template throw exception or something ...\n    }\n\n    if (! empty($match[2]) &amp;&amp; $match[1] == \"date\") {\n        $value = date($match[2], $value);\n    }\n\n    return $value;\n}, $template);\n<\/code><\/pre>\n<p>Instead of using <code>date(m)<\/code> or <code>date(Y)<\/code> you could also do things like <code>date(d-m-Y)<\/code> using this snippet<\/p>\n<p>This has the disadvantage that you can format only the <code>date<\/code> variable using this mechanism. But with a few tweaks you can extend this functionality.<\/p>\n<p><strong>Note:<\/strong> If you use a PHP version below 5.3 you can&#8217;t use closures but you can do the following:<\/p>\n<pre><code>function replace_callback_variables($match) {\n    global $data; \/\/ this is ugly\n\n    \/\/ same code as above:\n\n    $value = isset($data[$match[1]]) ? $data[$match[1]] : null;\n\n    if (!$value) {\n        \/\/ undefined variable in template throw exception or something ...\n    }\n\n    if (! empty($match[2]) &amp;&amp; $match[1] == \"date\") {\n        $value = date($match[2], $value);\n    }\n    return $value;\n}\n\n$data = array('title'=&gt;'some title', 'date'=&gt;1350498600, 'story'=&gt;'Some story');\n$template = \"#title#, <br \/>#date(d)#<br \/> #date(m)#<br \/>#date(Y)#<br \/> #story#\";\n\/\/ pass the function name as string to preg_replace_callback\n$result = preg_replace_callback('\/#(\\w+)(?:\\\\((.*?)\\\\))?#\/', 'replace_callback_variables', $template);\n<\/code><\/pre>\n<p>You can find more information about callbacks in PHP here<\/p>\n","protected":false},"excerpt":{"rendered":"<p>i think it won&#8217;t work with str_replace easily so i&#8217;m going to use preg_replace $data = array(&#8216;title&#8217;=&gt;&#8217;some title&#8217;, &#8216;date&#8217;=&gt;1350498600, &#8216;story&#8217;=&gt;&#8217;Some story&#8217;); $template = &#8220;#title#, #date(d)# #date(m)##date(Y)# #story#&#8221;; $result = preg_replace_callback(&#8216;\/#(\\w+)(?:\\\\((.*?)\\\\))?#\/&#8217;, function ($match) use($data) { $value = isset($data[$match[1]]) ? $data[$match[1]] : null; if (!$value) { \/\/ undefined variable in template throw exception or something &#8230; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6320","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6320"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6320\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}