Jump to content

How can I replace "part of a line in a file?


elentz

Recommended Posts

I have a configuration file.  From time to time I need to edit a couple of lines.  The lines will be something like:

 

firmware.url = tftp://192.168.0.245/v48.2.4.2.56acd.fw

firmware.md5 = 123456789abcdes

 

I need to replace everything AFTER the = in both lines.  I found this but its not quite what I need.  

$oldurl = "firmware.url = (someway of matchine whatever comes after)";
$newurl = "firmware.url = $url";
$data = file_get_contents("/tftpboot/a000000000010.cfg");
$newdata = str_replace($oldurl, $newurl, $data);
file_put_contents("/tftpboot/a000000000010.cfg", $newdata);

With this I need to keep track of each old and each new.  I could do it by creating a text file and reading it for both the new and old, but there must be a replace function or a way to tell the %old url to match anything after the "=".

 

Thanks

Link to comment
Share on other sites

Take a look at preg_replace() instead of str_replace(). You can define a regular expression pattern to replace instead of a fixed string.

 

<?php

$oldurl = "firmware.url = (someway of matchine whatever comes after)";
$newurl = "something else";

echo "Current: {$oldurl}\n";
echo "Replaced: " . preg_replace('/(firmware.url = )(.*)/', "$1 {$newurl}", $oldurl);

?>
Output:

Current: firmware.url = (someway of matchine whatever comes after)
Replaced: firmware.url = something else

Link to comment
Share on other sites

Its' not quite a ini file There are no sections. Here is a sample:

 

features.dnd.off = *79
features.dnd.0n = *78
local_time.time_zone = -5
local_time.summer_time= 2
features.accept_sip_trust_server_only = 1
multicast.receive.ignore_dnd.priority = 0
multicast.receive_priority.enable = 0
distinctive_ring_tones.alert_info.1.ringer =
distinctive_ring_tones.alert_info.2.ringer =
distinctive_ring_tones.alert_info.3.ringer =
distinctive_ring_tones.alert_info.4.ringer =
distinctive_ring_tones.alert_info.5.ringer =
distinctive_ring_tones.alert_info.6.ringer =
distinctive_ring_tones.alert_info.7.ringer =
distinctive_ring_tones.alert_info.8.ringer =
distinctive_ring_tones.alert_info.9.ringer =
distinctive_ring_tones.alert_info.10.ringer =
firmware.url = 88888
firmware.md5 = 99999
 
 
There might be 100 lines.  I might be able to insert sections like [Firmware] so that the parse_ini_file would work.
 
I set this up
<?php

$oldurl = "firmware.url = tftp://192.168.0.20/firm.fw";
$newurl = "firmware.url = tftp://192.168.0.20/new.fw";

echo "Current: {$oldurl}\n";
echo "Replaced: " . preg_replace('/(firmware.url = )(.*)/', "$1 {$newurl}", $oldurl);

?>

and I get this result:  Current: firmware.url = tftp://192.168.0.20/firm.fw Replaced: firmware.url = tftp://192.168.0.20/new.fw firmware.url = tftp://192.168.0.20/new.fw

 

It does the replacement twice.  

 

Barand

 

Are you the guy who (ahem, MANY moons ago) created the cool php table creater?

Link to comment
Share on other sites

You don't NEED sections to use parse_ini_file(). You're just on your own for writing that data back into a file when there are changes. I wish there was a write_ini_file() or some equiv. I'm sure someone has written it. Something smart enough to leave the comments in place? I dunno...

 

Anyway, you don't need the config name in the new URL, because you're just going to copy what's there and then insert the new value.

 

So my code works with this:

 

$oldurl = "firmware.url = tftp://192.168.0.20/firm.fw";
$newurl = "tftp://192.168.0.20/new.fw";
If you want it to work the way you have, then use this line:

 

echo "Replaced: " . preg_replace('/(firmware\.url = )(.*)/', $newurl, $data);
Where $data is the contents of the file you read. Also note I added a \ before the period in firmware.url. If spaces around the = sign are not consistent, you can modify that pattern to make them optional or match other whitespace.

 

Do you need to capture the old value of the setting for some reason?

 

Taking this a bit further, as mentioned, I'd make use of parse_ini_file(). Then define a $newdata['firmware.url'] type of array that contains the new values you want. Then create a function that replaces the values, writes the new file and returns the old values (or true/false if you don't need them).

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.