elentz Posted October 4, 2017 Share Posted October 4, 2017 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 Quote Link to comment Share on other sites More sharing options...
Sepodati Posted October 4, 2017 Share Posted October 4, 2017 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 4, 2017 Share Posted October 4, 2017 What you have there is a typical ini file format. Take a look at parse_ini_file() which will put the file contents into an array for easy access. Quote Link to comment Share on other sites More sharing options...
elentz Posted October 4, 2017 Author Share Posted October 4, 2017 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 4, 2017 Share Posted October 4, 2017 One of my first forays into the world of PHP Quote Link to comment Share on other sites More sharing options...
Sepodati Posted October 4, 2017 Share Posted October 4, 2017 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). Quote Link to comment Share on other sites More sharing options...
elentz Posted October 4, 2017 Author Share Posted October 4, 2017 Thanks for the help Sepodati. I will read and reread your last post to try and absorb it. I see you are in Caro, I am in Midland. Small world! Quote Link to comment Share on other sites More sharing options...
Sepodati Posted October 5, 2017 Share Posted October 5, 2017 Small indeed. Let me know if you're ever up for a cigar in Bay City and I'll explain all of this in person. Quote Link to comment Share on other sites More sharing options...
elentz Posted October 5, 2017 Author Share Posted October 5, 2017 Ha, Don't smoke , but maybe some day. What do you do for a living? My company manufactures and installs business phone systems and provide IT services. Quote Link to comment Share on other sites More sharing options...
elentz Posted October 5, 2017 Author Share Posted October 5, 2017 I got it setup and it works exactly as I need it. Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.