Overwrite content in PHP fwrite ()

Is it possible to rewrite a string in PHP. let me be more clear using examples.

My array

array{
    [DEVICE] => eth0,
    [IPADDR] => 192.168.0.2,
    [NETMASK] => 255.255.255.0,
    [NETWORK] => 192.168.0.0,
    [BROADCAST] => 255.255.255.255,
    [GATEWAY] => 192.168.0.1,
    [ONBOOT] => no
}

Transfer files to file

DEVICE=eth0
IPADDR=192.168.200.2
NETMASK=255.255.255.0
NETWORK=192.168.200.0
BROADCAST=255.255.255.255
GATEWAY=192.168.200.1
ONBOOT=no
DNS1=195.100.10.1

The result of the file that is overwritten

DEVICE=eth0
IPADDR=192.168.0.2
NETMASK=255.255.255.0
NETWORK=192.168.0.0
BROADCAST=255.255.255.255
GATEWAY=192.168.0.1
ONBOOT=no
DNS1=195.100.10.1

Note that DNS1=195.100.10.1Stays in the file, because the associate professor has a key with a value DNSin our array.

thank

+3
source share
3 answers

You need to find an algorithm for this:

  • Read the file to be overwritten with the function file()(look carefully at the parameters, you do not want the char news line to be included in the array).
  • Swipe through the array and explode()each element to create a new array of keys =>.
  • .
  • , \n
+2

, , preg_replace, , .

+2

I am the second webbiedave comment. You can only search for a suitable position, and then use fwrite if what you write is the same size as you (it is not).

+1
source

Source: https://habr.com/ru/post/1745466/


All Articles