Jump to content

[SOLVED] Replace all from this to that. - Flat files


aQ

Recommended Posts

Hello!

 

I am experimenting a little using Flat files now. I have a file that looks something like this:

<?php
// variable
$var = "asdasd";

// array
$array = array("lala", "lalal");
?>

 

How can I replace the $array's content with for example "array("blablabla", "blabla")" without knowing the old value of it?

I think what you're after is something like:

 

<?php
//$file = file_get_contents('yourfile');
$file = '<?php
// variable
$var = "asdasd";

// array
$array = array("lala", "lalal");
?>';
echo '<textarea rows=10 cols=30>'.$file.'</textarea>';
$array_to_replace = "array";//the name of the array you are wanting to replace
$replace_with = '$'.$array_to_replace.'= array("blabla","blahaha");';//whatever you want to replace with
$file = preg_replace('|\$'.$array_to_replace.'(.*?);|s',$replace_with,$file);
echo '<textarea rows=10 cols=30>'.$file.'</textarea>';
//write new file with $file;
?>

 

As you can see, i tested it with a normal string - but if you use file_get_contents you can read the file into a string anyway, so just modify that bit. And then you'll have to write back to the file.

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.