Jump to content

If str_replace?


JREAM

Recommended Posts

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

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

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

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.