Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In php, how do I load txt file as a string?

Tags:

php

I have a text file that contains paragraphs of text. I want to load it as a string in php, and then collect all the e-mail addresses within it as an array. However, how do I initially load the text file contents as the string? Restated, I have

$string = **WITHIN TEXT FILE "[email protected] MIME-Version: bla bla bla [email protected]";
$matches = array();
$pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'
preg_match($pattern, $string, $matches);

How do I load the text from the text file into the string variable?

Thanks in advance!

like image 887
HumbleHelper Avatar asked Oct 10 '10 15:10

HumbleHelper


People also ask

How do I read a .TXT file in PHP?

PHP Read File - fread() The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

How do I save a text file as a variable?

How i can store text file data into a variable? You can use the variable operation command. You can use the command read from text file and move the data into the variable. Also, you can read from txt file using a system variable and then store it to different variable using Variable operation command.

How do you open a file and write in PHP?

PHP Create File - fopen() If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How do I display the contents of a file in PHP?

The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out. <? php echo file_get_contents( "filename.


2 Answers

$fileContent = file_get_contents($url);

Check the reference at http://php.net/manual/en/function.file-get-contents.php

like image 50
Bang Dao Avatar answered Oct 19 '22 08:10

Bang Dao


The simplest way is file-get-contents!

$file_content = file_get_contents('/path/to/file');
like image 22
Aif Avatar answered Oct 19 '22 07:10

Aif