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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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