JREAM Posted September 9, 2008 Share Posted September 9, 2008 Hey, I have 2 arrays, one to find a string, another to replace it -- if they match When $encoded[0] matches a str_replace it swaps with $decoded[0], it all works well here. My problem is I want the $decoded to include a file when it replaces the text -- I had this working with the string commented out, but it would instead include every part of the array. I am looking to see how I would make the str_replace($encoded[?], $decoded[?], $file); match up for inclusion. I also tried str_replace($encoded, include($decoded), $file); but this tries to include "Array". I also tried an if(str_replace($decoded[0]) { //do something;} but couldnt get that rolling, I had also tried making $decoded a case/switch but that wouldn't work either. Does anyone have an idea how I can make this work, Ive been at it about 5 hours and starting to lose hope! $encoded = array ( '###block-1###', '###block-2###', '###block-3###', ); $decoded = array ( // include 'block-1.php'; // can't get individual ones to include or they all will be included 'block-1.php', 'block-2.php', 'block-3.php', ); $file = file_get_contents($contentFile); echo str_replace($encoded, $decoded, $file); Link to comment https://forums.phpfreaks.com/topic/123433-if-str_replace/ Share on other sites More sharing options...
Mchl Posted September 9, 2008 Share Posted September 9, 2008 I'm not sure I get what you trying to achieve. Perhaps this? $encoded = array ( '###block-1###', '###block-2###', '###block-3###', ); $decoded = array ( "include('block-1.php')", "include('block-2.php')", "include('block-3.php')", ); $file = file_get_contents($contentFile); echo $file = str_replace($encoded, $decoded, $file); Or this? $encoded = array ( '###block-1###', '###block-2###', '###block-3###', ); $decoded = array ( 'block-1.php', 'block-2.php', 'block-3.php', ); $file = file_get_contents($contentFile); echo $file = str_replace($encoded, $decoded, $file); inlude($file); ?? Link to comment https://forums.phpfreaks.com/topic/123433-if-str_replace/#findComment-637504 Share on other sites More sharing options...
discomatt Posted September 9, 2008 Share Posted September 9, 2008 Some sample data from the file you're trying to include would help. Perhaps an expected output as well. Link to comment https://forums.phpfreaks.com/topic/123433-if-str_replace/#findComment-637516 Share on other sites More sharing options...
aschk Posted September 9, 2008 Share Posted September 9, 2008 What you need to do is parse the contents of each of those blocks before you insert them. See the following: <?php $encoded = array ( '###block-1###', '###block-2###', '###block-3###', ); $decoded = array ( 'block-1.php', 'block-2.php', 'block-3.php', ); $file = file_get_contents($contentFile); for($i=0; $i < count($encoded); $i++){ $enc = $encoded[$i]; ob_start(); include($decoded[$i]); $tmp = ob_get_contents(); ob_end_clean(); $file = str_replace($enc, $tmp, $file); } echo $file; ?> Link to comment https://forums.phpfreaks.com/topic/123433-if-str_replace/#findComment-637521 Share on other sites More sharing options...
JREAM Posted September 9, 2008 Author Share Posted September 9, 2008 ashck == thanks !! you understood what I ment, that does work, does that buffering help with speed or sometihng? Link to comment https://forums.phpfreaks.com/topic/123433-if-str_replace/#findComment-637546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.