Jump to content

[SOLVED] Replace text with page


Brandon Jaeger

Recommended Posts

How would I take text from a string and replace it with an page (include it)?

 

For example, I'm using regex to find the string:

$str = "This {INCLUDE:page_01.php} is {INCLUDE:page_02.php} some {INCLUDE:page_03.php} text";
preg_match_all("#{INCLUDE:(.*?)}#is", $str, $matches);

 

I want to somehow include the page from the matches exactly where it's located in the string.

 

Is it possible?

 

Thanks,

Brandon

Link to comment
Share on other sites

So the file has what, just ordinary text in it or something? You could do it like this:

 

$str = "This {INCLUDE:page_01.php} is {INCLUDE:page_02.php} some {INCLUDE:page_03.php} text";
preg_match_all("#{INCLUDE:(.*?)}#is", $str, $matches);

for($i = 0; $i < count($matches[0]); $i++){
$str = str_replace($matches[0][$i], file_get_contents($matches[1][$i]), $str);
}

echo $str;

Link to comment
Share on other sites

I'm a little tired, so I apologize if this is a weak answer:

 

<?php
$a = explode('{INCLUDE:',$str);
foreach ($a as $i){
$p = strpos($i,".php");
$p = $p+4;
$include = substr($i,0,$p);
include_once($include);
}
?>

 

Maybe that?

Link to comment
Share on other sites

Nope. Thanks anyway... But what I'm trying to achieve is simply replacing that text with the file itself.

 

I might use ProjectFear's method like so:

$str = "This {INCLUDE:page_01.php} is {INCLUDE:page_02.php} some {INCLUDE:page_03.php} text";
preg_match_all("#{INCLUDE:(.*?)}#is", $str, $matches);

for($i = 0; $i < count($matches[0]); $i++){
  $str = str_replace($matches[0][$i], "<?php include(\"" . $matches[1][$i] . "\"); php?>", $str);
}

echo $str;

Would something like that work if the parent page is .php?

Link to comment
Share on other sites

What I was wondering is what is IN the file. If you just want to replace the {INCLUDE:file.php} with the contents of file.php then what I posted should do the trick.

 

That code you posted above is different to mine. I used file_get_contents() to get the contents of the file. I don't know if what you have above would work.

Link to comment
Share on other sites

Just when I thought I had it solved, I run into yet another problem...

 

	$str = "This {INCLUDE:test.php} is {INCLUDE:page_02.php} some {INCLUDE:page_03.php} text";

$str = str_replace(" ", "<br \>", $str);

preg_match_all("#{INCLUDE:(.*?)}#is", $str, $matches);

$numMatches = count($matches[0]);

for($i = 0; $i < $numMatches; $i++)
	if(file_exists($matches[1][$i]))
		$str = str_replace($matches[0][$i], include($matches[1][$i]), $str);
	else
		$str = str_replace($matches[0][$i], "Couldn't open <b>" . $matches[1][$i] . "</b>", $str);

echo $str;

The first file exists on the server, the other 2 do not.

 

Contents of test.php is a simple 'hello world' script.

 

This is what it prints out:

Hello, world!This

1

is

Couldn't open page_02.php

some

Couldn't open page_03.php

text

 

Notice the positioning and the extra "1".

 

Is there a way to fix this?

 

P.S. This is probably what you're talking about, ProjectFear.  ???

Link to comment
Share on other sites

There is probably a better way to achieve what you are trying to do but here is a way of doing it.

I'm assuming your page_01.php page looks like this:

 

<?php
echo "Hello, world!";
?>

 

If it does make it like this:

 

<?php
$return = "Hello, world!";
?>

 

Then you can do this:

 

<?php

$str = "This {INCLUDE:page_01.php} is {INCLUDE:page_02.php} some {INCLUDE:page_03.php} text";
preg_match_all("#{INCLUDE:(.*?)}#is", $str, $matches);

for($i = 0; $i < count($matches[0]); $i++){
if(file_exists($matches[1][$i])){
	include($matches[1][$i]);
	$str = str_replace($matches[0][$i], $return, $str);
}else{
	$str = str_replace($matches[0][$i], "Failed to include <b>".$matches[1][$i]."</b>", $str);
}
}

echo $str;

?>

Link to comment
Share on other sites

Not exactly what I'm looking for, but I think I'm going to use it anyway. I wanted it to be able to include ANY PHP script that I'd stick in there, but I think I'm just going to take the PHP scripts, add all of the text to a string, and replace it from there like you just showed me.

 

Thanks a ton! I'll post again if I have anymore problems.

Link to comment
Share on other sites

Full code that works 100% (so far at least!):

 

<?php
$str = "This {INCLUDE:test.php} is {INCLUDE:test.html} some {INCLUDE:test.txt} text";

$str = str_replace(" ", "<br \>", $str);

preg_match_all("#{INCLUDE:(.*?)}#is", $str, $matches);

$numMatches = count($matches[0]);

for($i = 0; $i < $numMatches; $i++)
{
$file_name = $matches[1][$i];

if(file_exists($file_name))
{
	if(strpos(GetExt($file_name), ".php") !== FALSE)
	{
		include($file_name);
		$str = str_replace($matches[0][$i], $content, $str);
	}
	else
		$str = str_replace($matches[0][$i], file_get_contents($file_name), $str);
}
else
	$str = str_replace($matches[0][$i], "Couldn't open <b>" . $file_name . "</b>", $str);
}

echo $str;

function GetExt($file)
{
$pos = strrpos($file, '.');
$str = substr($file, $pos);

return strtolower($str);
}
?>

Content of test.php:

<?php
$content = "Hello, world!";
?>

 

This is for reference in case anyone brings up this topic in a search and needs the full code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.