I have been learning php for the first time, and I want to write a script to replace manual operations. Today, a simple script is completed: replace the string about numbers in the file with random numbers. This script mainly applies the following points: 1) reading and writing Files 2) generating random numbers 3) replacing
It is mainly because replacement involves regular expressions.
I. file read/write
// Read each row in the file
// Write $ str to the file
$ Fp1 = fopen ($ filename1, "r ");
$ Fp2 = fopetn ($ filename2, "w + ")
While (! Feof ($ fp1 ))
$ Str = fgets ($ fp1 );
Fwrtie ($ fp2, $ str );
II. generation of random numbers
Rand (1,100)
Random integer between 1-
III. replacement
Preg_replace (): Searches and replaces regular expressions.
Preg_replace ($ pattern, $ replacement, $ str)
$ Pattern: Search mode
$ Replacement: replacement Character
$ Str: string to be searched and replaced