Brandon Jaeger Posted October 4, 2008 Share Posted October 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 4, 2008 Share Posted October 4, 2008 If you have already extracted the file name, just assign it to a variable, then include that variable. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 I know that... but I'm having a problem replacing it in the string exactly where its at. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 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; Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 It could be any type of file. I'm specifically aiming at including .html and .php files. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 4, 2008 Share Posted October 4, 2008 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? Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 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? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 It could be anything. An HTML file; PHP script; etc. Would your method allow the execution of a PHP script? Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 Solved it! Solution: $str = str_replace($matches[0][$i], include($matches[1][$i]), $str); I didn't think that was possible, but it is! Thanks a lot for your help F1Fan and ProjectFear Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 I'm confused, wouldn't that just replace it with a 1 depending on if the file was successfully included, although the contents of the file will still be displayed but they wouldn't be at the position you want them at. ??? Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 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. ??? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted October 4, 2008 Author Share Posted October 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.