Forum Moderators: coopster
Below is my code for performing one find and replace.
<?php
function myfunction() {
include 'http://www.example.com/cgi-bin/topnav.php';
}
ob_start();
include 'ad_index.php';
$page = ob_get_contents();
ob_end_clean();ob_start();
myfunction();
$mystuff = ob_get_contents();
ob_end_clean();
echo str_replace( "***topnav***", $mystuff, $page );
?>
Any help on squeezing another one in there would be appreciated :)
[edited by: eelixduppy at 9:51 pm (utc) on Dec. 3, 2007]
[edit reason] use example.com, please [/edit]
I have tried almost 200 different combinations, and still I cannot get it. Very challenging to me, even though I am assuming it is simple and am just missing it.
function top_nav($buf) {
$replace = // whatever
$buf = str_replace( "***topnav***", $replace, $buf);
return $buf;
}
function footer($buf) {
// another find and replace
return $buf;
}
ob_start(); // main
ob_start('top_nav'); // top_nav
echo $page;
ob_start('footer'); // footer
echo $footer;
ob_end_fluch(); // footer
ob_end_flush(); // top_nav
ob_end_flush(); // main
So basically it does not allow me to have two functions. I have tried just copying and pasting my orignal PHP code again below the top nav, then changing the information to my footer info, but then it repeats the page twice on the same page.
So the first display of my page includes the top nav, but no footer, and the next one has no top nav, but has my footer. This is because I tell the PHP code to include the ad_index.php file as the whole page for both.
So since I just copy and pasted it, it spits out the page twice. But of course if I get rid of the include ad_index.php in the footer links part, it doesn't make the page repeat, and the top nav displays fine, but at the bottom it gives me that same error saying it cannot redeclare the function.
Very strange.
Also my version of the buffering using multiple calls wouldn't work, as you need to finish each set of find and replace before calling the next one. Otherwise the first set of code is going to go through the next set of find and replace as well. However as you are searching for something very specific this probably wouldn't affect the outcome, however the example is incorrect. Sorry.
Does a find and replace mean that regular expressions have to be used?
No, none of my includes have another function in them. Basically all they have is standard HTML.
Also, I tried to change your code to do both the top nav and footerlinks in 2 separate PHP calls, but instead of getting the topnav and no footer this time, I get neither the topnav or footer.
So I guess I am back to square one. Oh well, it really is not that important, it just means that whenever I update my footer I have to remember to do it twice. Would be nice to know how though for future reference :)
str_replace is great when you know what you are looking for, but to match every single version of <a href="SOMETHING" you would need a number of combinations that are far to large for me to fit on this page; however '%<a href="\w+"%' will match any number of SOMETHING's in 1 combination.