Forum Moderators: coopster

Message Too Old, No Replies

Find and Replace with PHP Include

Find and Replace with PHP Include

         

GalaxiDesigns

8:31 pm on Dec 3, 2007 (gmt 0)

10+ Year Member



Hey all. I have a real simple question about doing a find and replace in PHP. Basically I have already figured out how to do it for one PHP include, but now I need to get it to include another one.

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]

PHP_Chimp

8:31 am on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cant you just use -

myfunction() {
include '1_file.php';
include 'another_file.php';
}

As then both of these files should end up in the buffer?

GalaxiDesigns

5:09 pm on Dec 4, 2007 (gmt 0)

10+ Year Member



Yeah, but I need it to do another find and replace. You see, in the original one it finds and replaces ***topnav*** with that PHP include. Now I need it to find and replace the other include with another string of text, this time ***footer*** though.

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.

eelixduppy

5:18 pm on Dec 4, 2007 (gmt 0)



Do you actually have any php code in either of the included files?

GalaxiDesigns

6:31 pm on Dec 4, 2007 (gmt 0)

10+ Year Member



No, the PHP includes just have standard HTML in them.

PHP_Chimp

12:00 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One method that you could use is nested ob_start calls.
As you could then pass each include through a different find and replace function.
i.e.

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

GalaxiDesigns

9:03 pm on Dec 5, 2007 (gmt 0)

10+ Year Member



Thanks Chimp for your help, but I still could not get it to work. It basically gives me the same error I always get when I was trying to include two PHP includes, "Cannot redeclare function"

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.

eelixduppy

9:18 pm on Dec 5, 2007 (gmt 0)



You should just use file_get_contents [us2.php.net], do the string replacement, and then echo out the result. You shouldn't need to be using output buffers to do this.

PHP_Chimp

8:29 am on Dec 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the include files do you have functions named the same thing? Hence getting a "Cannot redeclare function" warning.
As when you bring these functions into the same page they get the same scope, so you cant have 2 functions with the same name. Well at least this is what I have always assumed, so have never tried to have 2 functions with the same name in different includes.

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.

Marcia

8:57 am on Dec 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to figure out how to find links with certain patterns in URLs on sites, and use PHP to replace them either with a jump script using a flatfile or an array, or simply just add rel="nofollow" within the <a href= right before the > character.

Does a find and replace mean that regular expressions have to be used?

GalaxiDesigns

7:45 pm on Dec 6, 2007 (gmt 0)

10+ Year Member



PHPChimp:

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 :)

PHP_Chimp

7:48 pm on Dec 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You dont have to use regular expressions, however as you are looking for links that match a certain pattern it would be almost impossible not to use a regular expression.

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.