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
https://forums.phpfreaks.com/topic/126981-solved-replace-text-with-page/
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;

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?

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.

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.  ???

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;

?>

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.