Hi,

I am using str_replace to scan through pages and replace, for example; [gallery] with the actual gallery page using an include command.

For example;

[gallery]

Once this is located it's replaced by
include('modules/gallery/index.php');

How can you do this (and for multiple ones, [gallery], [enquiry]?

Thank's very much.
Zack.

Recommended Answers

All 20 Replies

str_replace is for strings. I don't think you can replace it with php commands as it would be placed in a string container.

What command would i need to replace with php code?

Kind regards,
Zack.

$str1 = array('[gallery]','[enquiry]');
$str2 = "include('modules/gallery/index.php');";
str_replace($str1, $str2, $target);

This will do the same for you, provided $target is the target string inside which you want to do the replacement.
try to escape the '/' inside the string if it troubles

Hi,

This is the current code concept i have come up with:

$code  = $pager['code'];
	$block = array("[gallery]", "[enquiry]");	
	$replace   = array("include('modules/gallery/index.php')", "include('modules/enquiry/index.php')");
	$replaced_code = str_replace($block, $replace, $code);
	echo $replaced_code;

Say i use the [gallery] tag, it just displays;

include('modules/gallery/index.php')

Zack

<?php

 $file_path = "gallery/index.php";
 
 /* First Include Gallery Module */
 
 include ($file_path);
  
 /* Replace Gallery Module to Enquiry Module */

 $file_path = str_replace("gallery","enquiry",$file_path);
 
 /* First Include Gallery Module */
 
  include ($file_path);

 
?>
<?php

 $file_path = "gallery/index.php";
 
 /* First Include Gallery Module */
 
 include ($file_path);
  
 /* Replace Gallery Module to Enquiry Module */

 $file_path = str_replace("gallery","enquiry",$file_path);
 
 /* First Include Gallery Module */
 
  include ($file_path);

 
?>

Hiya,

Bit confused, how would that include the enquiry php file for [enquiry] and the gallery php file for [gallery]?

Thanks
Zack

go ahead with your code, it should work, similar as mine

Hi Network18,

The code i provided does not work. It just displays include('filepath'); on the webpage, it does not include the file.

Zack

You would need to run the new string through eval to make the includes work.

Use that function with caution though. If you don't use it right there are huge security risks.

You would need to run the new string through eval to make the includes work.

Use that function with caution though. If you don't use it right there are huge security risks.

can you post the part of your code here, we will modify it

<?php

 $file_path = "modules/gallery/index.php";
 
 include ($file_path);
 
 $file_path = str_replace("gallery","enquiry",$file_path);
 
 include ($file_path);
 
?>
<?php

 $file_path = "modules/gallery/index.php";
 
 include ($file_path);
 
 $file_path = str_replace("gallery","enquiry",$file_path);
 
 include ($file_path);
 
?>

Is [GALLERY] inside <?php ?> tags?

I would manually code the include where you want.

One way would to be use authoring software like DW to search and replace [GALLERY] with <?php include("gallery/index.php"); ?>

also kkeith29's idea is a great possibility, but comes with risks.

Hi Guys,

Thank's for all your help so far.

Sure. I have 2 files pulling this together so far.

index.php

include('includes/global.php');
DisplayIndex();

global.php

function DisplayIndex() {

	ConnDB();

	$index = mysql_query("SELECT * FROM pages WHERE url='index' AND page_group='0'") 
	or die(mysql_error());

		while($pager = mysql_fetch_array( $index )) {

			
			include('build.php');
			include('_lang.php');
			include('seo.php');
			include('design/1.php');
				
				echo $pager['code'];

			include('design/2.php');

		}

}

As you can see, the content is stored in a mysql database.

What i would like is that when someone stores somthing like this in the database:

Contact Us
[enquiry]

Or browse view our gallery
[gallery]

They will appear from an includes. $pager is pulling the content for the page.

Zack.

Hmmm, am I missing something as I don't understand why you are doing it that way why not make your path then include it...

$path = "includes/" . $pager['code'] . "/index.php";
include( $path );

As an example.

As I said I might have missed something.

Hi,

Basically a client can login and update the content of there webpage. This content is saved to a mysql database.

Onto the next part, when the mysql database content is pulled and displayed on the actual webpage during a echo i need to be able to scan for tags, for example [enquiry] or [gallery] and include the relevent file for that tag.

Let's say the customer udpates the content of there index page to say "This is my index page. Why not email me using our contact form [enquiry]" then we need to include enquiry.php.

Currently everything works apart from the whole issue with searching and replacing tags with the relevent include file.

Zack.

This would be very annoying as you would need to stop outputting the database information, place the include(); and then start echoing out the rest.

I would have no idea where to start with something like that.

One idea would be to load both pages into a variable at the start names $enquiry and $gallery and output them that way.

I think Regex is more appropriate for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.