First contact with php

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.