JKG Posted April 20, 2011 Share Posted April 20, 2011 hello, I am working on a site and have hit a bit of brick wall. I use a little custom CMS i have built up and for this project i need to have a lot of different menus which i need to call from the CMS, which runs from a database. Basically, at the minute i include them from the page, but would like it to be controlled through the CMS via place holders eg: {{rightHandMenu}} Is there something that i can do so when the data is pulled from the db i use maybe a find a replace, that can sniff out that phrase, then parse with php to be eg: include 'blocks/rightHandMenu.php'; Im on a shared server and dont want to rewrite the whole site so i presume smarty is out of the window...? Sorry its a bit wordy. Thanks for reading!! Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted April 20, 2011 Share Posted April 20, 2011 It's not too wordy... detail is good when you want help. Check out preg_replace. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 20, 2011 Share Posted April 20, 2011 Or str_replace(), if you are replacing fixed strings with other fixed strings. You would most likely read in all the replacement values and use str_replace() on each one if using that approach. preg_replace() has more advanced features, such as being able to call a function for each replacement, with the 'e' modifier. That would allow you to include a different file depending on which tag was found, for example. Quote Link to comment Share on other sites More sharing options...
JKG Posted April 21, 2011 Author Share Posted April 21, 2011 thanks alot guys. could you possibly please give me some idea of how to implement? obviously it would be something along these lines: $placeholders = array('{{rightHandMenu}}', '{{leftBox}}'); $vals = array('include1', 'include2'); $str = str_replace($placeholders, $vals, $body); but how do i do the includes? i cant store them in a variable, and putting them straight in the array comes back with an error. i am unfamiliar with preg_replace, but am going to research today. Thanks! Quote Link to comment Share on other sites More sharing options...
JKG Posted April 21, 2011 Author Share Posted April 21, 2011 ah sorted it! thanks for you help guys for any one else who wants to know: the above does work with include strings, must have had a syntax error in it before. thanks for pointing me in the right direction. Quote Link to comment Share on other sites More sharing options...
JKG Posted April 21, 2011 Author Share Posted April 21, 2011 oh, maybe not. i think my page is performing the include within the array. <? $placeholders = array('{{leftHandMenu}}', '{{leftBox}}'); $includes = array(include $root_dir . $html_dir . $includes_dir . 'blocks/leftHandMenu.php', 'include2'); echo str_replace($placeholders, $includes, stripslashes($body)); ?> so when it reads the include statement, it actually parses it and performs the include, then when it reads the str_replace it just outputs 1. any thoughts? thanks. Joe. Quote Link to comment Share on other sites More sharing options...
JKG Posted April 21, 2011 Author Share Posted April 21, 2011 would ob_get_clean or eval do anything for me here? been playing around but cant get it to work.. sorry for dragging this out! Quote Link to comment Share on other sites More sharing options...
JKG Posted April 21, 2011 Author Share Posted April 21, 2011 sorted. but it isnt pretty!! ill just dump the code here for anyone who wants to know: $placeholders = array('{{leftHandMenu}}', '{{leftBox}}'); //what you want to find $includes = array(getLefthandMenu($body), 'include2'); //what you want to replace with, respectively echo str_replace($placeholders, $includes, stripslashes($body)); //perform find and replace functions.php function getLeftHandMenu($body) { if(strpos($body, '{leftHandMenu}}')){ //had to miss out the first '{' cos otherwise strpos returns 0, which seems false $leftHandMenuFile = '/fullpath/includes/blocks/leftHandMenu.php'; //full path to file you want to include $leftHandMenuFileOpen = fopen($leftHandMenuFile, 'r'); //open with read perms $leftHandMenu = fread($leftHandMenuFileOpen, filesize($leftHandMenuFile)); //read file $evalLefthandMenu = eval('?>'.$leftHandMenu); //if file has php, use this to parse the php fclose($leftHandMenuFileOpen); //good practise to close file after use echo $evalLefthandMenu; //return the findings! } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.