954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

str_replace help needed

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.

Zack_G
Newbie Poster
9 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

liamfriel
Junior Poster
103 posts since Oct 2009
Reputation Points: 13
Solved Threads: 13
 

What command would i need to replace with php code?

Kind regards,
Zack.

Zack_G
Newbie Poster
9 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
$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

network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 

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

Zack_G
Newbie Poster
9 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
<?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);

 
?>
alagirinetaxis
Newbie Poster
15 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 
<?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);

 
?>
alagirinetaxis
Newbie Poster
15 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

Hiya,

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

Thanks
Zack

Zack_G
Newbie Poster
9 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 

Hi Network18,

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

Zack

Zack_G
Newbie Poster
9 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

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.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

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

network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 
<?php

 $file_path = "modules/gallery/index.php";
 
 include ($file_path);
 
 $file_path = str_replace("gallery","enquiry",$file_path);
 
 include ($file_path);
 
?>
alagirinetaxis
Newbie Poster
15 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 
<?php

 $file_path = "modules/gallery/index.php";
 
 include ($file_path);
 
 $file_path = str_replace("gallery","enquiry",$file_path);
 
 include ($file_path);
 
?>
alagirinetaxis
Newbie Poster
15 posts since Oct 2009
Reputation Points: 10
Solved Threads: 3
 

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.

liamfriel
Junior Poster
103 posts since Oct 2009
Reputation Points: 13
Solved Threads: 13
 

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['code'] is pulling the content for the page.

Zack.

Zack_G
Newbie Poster
9 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Froger93
Junior Poster in Training
74 posts since Sep 2009
Reputation Points: 11
Solved Threads: 6
 

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.

Zack_G
Newbie Poster
9 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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.

liamfriel
Junior Poster
103 posts since Oct 2009
Reputation Points: 13
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You