PHP 4 Common ways to read text files and the Str_replace parameter meaning

Source: Internet
Author: User
This article mainly for you in detail the PHP read text files commonly used 4 ways and str_replace in the parameters of the meaning of not commonly used, with a certain reference value, interested in small partners can refer to

Seemingly simple things, but it is likely that you do not know enough to know more of course you will not find it complicated, but you know more than others. Like our usual file_get_contents we often use it to fetch a URL content, but actually it can also be used to perform post requests, with the above information, set timeout, and even can call FTP, specify proxy and so on. Even just like the simple echo function, you don't know more, and maybe you don't have the ability to write multiple variables directly behind Echo, just by separating them with commas.

Today accidentally turned a file method in the manual, I have not used this method before, I feel very useful. You can read the contents of a text file directly into an array in a row, and it feels very convenient in some cases. Then I saw a bit more PHP operation file method, and then in the use of str_replace this often used method, and in the use of str_replace, I was wondering if I can pass parameters to control the loop replacement.

For example, a string:Hello World, I want to replace lo with ol. After the substitution it became Helol world, and after the replacement it had the LO in its structure, could it be controlled to replace it again. However, this parameter is not found. The str_replace itself has a 4th parameter, but it is a return data that returns the total number of substitutions that occurred during the replacement period. Today's play is organized as follows:

Str_replace is organized as follows:
1:str_replace: The last parameter returns a statistic that has been replaced by a total number of times.
2:str_replace: Will loop replace
3:str_replace: Operations can be replaced directly on an array, without the need for loops to be replaced separately
4: A very nice replacement: Use an array that contains the one you want to replace and the

//1:str_replace: The last parameter returns a statistic that has been replaced by a total number of times. $char = Str_replace (Array (' is ', ' S '), ' _ ', ' This is a replace test. ', $count); Echo $char. ' co-replacement '. $count. ' $char ';//2:str_replace: cyclic replacement = Str_ Replace (Array (' t ', ' K '), Array (' K ', ' _ '), ' This is a replace test. ', $count); Echo $char. ' Total substitution '. $count. ' Times <br> ';//3: Str_replace: Can be replaced directly on the array, do not need to replace the loop $char_arr = Array (' Hello ', ' world '), $char _arr = str_replace (Array (' O ', ' K '), array (' K ', ' _ '), $char _arr, $count);p Rint_r ($char _arr); Echo ' total substitution '. $count. ' Times <br><br> ';//4: A very nice replacement: Use an array of $ Replacethis = Array (' old word ' = ' new word ', ' is ' = ' it ', ' past ' = ' future ',); $originalText = "Ever Y old word is a thing of the past ... "; $replacedText = Str_replace (Array_keys ($replaceThi 


4 ways to read PHP files:
1, you can read the file one time using Fopen,fread, or multiple times by specifying the size.
2, using fopen to open, read by fgets, fgets do not specify the length parameter, the default is to read 1k.
3, use the file function to read the contents into an array (separated by rows)
4, how to read the contents of a file file_get_contents

 $file _path = "Error.log", if (!file_exists ($file _path)) exit (' File not exits. '); /1, use Fopen,fread to read the file one time, or by specifying the size multiple times. $fp = fopen ($file _path, "R"), $str = Trim (Fread ($fp, FileSize ($file _path)), Echo str_replace ("RN"), "<br>", $STR); echo "<br>". Str_repeat ('-', 50). " <br> ";//2, open with fopen, read line by fgets, fgets do not specify the length parameter, read 1k by default. $fp = fopen ($file _path, "R"), $str = "", while (!feof ($fp)) {$str. = fgets ($fp);} $str = Str_replace ("rn", "<br>", $str); Echo $str;//3, using the file function to read the contents into an array (separated by rows) $file = file ($file _path); echo Implode (' <br> ', $file); echo "<br>". Str_repeat ('-', 50). " <br> ";//4, how to read the contents of a file File_get_contents$str=trim (file_get_contents ($file _path)); Echo str_replace (" RN ") , "<br>", $str), echo "<br>". Str_repeat ('-', 50). " <br> "; 

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.