PHP str_replace with the contents of a file -
i'm wanting create short codes wordpress uses. when user puts [related] post, show content inside include "includes/related_article_inline.php" instead of [related]. i've tried far is:
$searchstring = '[related]'; $replacementstring = '<?php echo include "includes/related_article_inline.php"; ?>'; echo str_replace( $searchstring ,$replacementstring ,$post_content ); but, isn't quite working properly. know how can replace [related] contents of includes/related_article_inline.php?
str_replace doesn't include file, simple string-replacement. browser show
include "includes/related_article_inline.php";
somewhere, @ position added [replace].
two possible solutions.
1) if know on forehand [replace] in post-content: include before str_replace, store output string (using output-buffering), , actual replacement.
ob_start(); // start output buffering include "includes/related_article_inline.php"; $replacementstring = ob_get_clean(); // store output in variable, , stop output-buffering echo str_replace($searchstring, $replacementstring, $post_content); 2) if don't know on forehand [replace] in post-content, don't want include it. use preg_replace_callback file included if necessary.
$replacementfunction = function() { ob_start(); // start output buffering include "includes/related_article_inline.php"; return ob_get_clean(); // return output, , stop output-buffering }; $searchregex = '/\[replace\]/'; // have rewrite search-string regular expression echo preg_replace_callback($searchregex, $replacementfunction, $post_content);
Comments
Post a Comment