-1

i know this will be easy for you but not for me :)

i need to get the contents of txt file "file.txt"

file.txt contetnt is

word1 word2 word3

each in a new line or can be as word1, word2, word3, or "word1", "word2", "word3", whichever is easier to do.

i have config.php file which i need it to read all and every word in file.txt and process as $value to be then processed by script.php which process $value as a single word of all the words in file.txt

thanks

Ayman
  • 131
  • 1
  • 9

3 Answers3

1

Assuming your file.php is as

<?php
    $words = array("word1", "word2", "word3");
 ?>

In another php file , say read.php, you can access file.php as

<?php

include "file.php";
print_r($words);
foreach($words as $value){
    echo $value;
//do whatever you want to do with each word
}

?>
Mad Angle
  • 2,347
  • 1
  • 15
  • 33
  • is there a way to get words from `file.php` which uses the following format `var word = ["word1", "word2",];` instead ? – Ayman Aug 06 '14 at 12:16
  • you need to get all the words of the file.php in an array? right? – Mad Angle Aug 06 '14 at 12:17
  • Using regexes like that?! This would have been my last solution. – machineaddict Aug 06 '14 at 12:18
  • Did you try my code?? I think it satisfies your need. – Mad Angle Aug 06 '14 at 12:18
  • @BlankHead, i'm sure it works but since i already have a php file it would be much better to use than to create another txt file and use sorry for the trouble – Ayman Aug 06 '14 at 12:21
  • ok, i have a `file.php` which have the following contents `var words = ["word1", "word2", "word3",];` lets say infinite number of words, i need php to read each and every word from `var words =` and treat them as `$value` – Ayman Aug 06 '14 at 12:26
  • @BlankHead, my php file is exactly as `var words = ["word1", "word2", "word3",];` – Ayman Aug 06 '14 at 12:36
  • You can't store the value like that in a php file. Its wrong format. In javascript file we use like this. (it won't support in php5 and above.) ref: http://stackoverflow.com/questions/1206105/what-does-php-keyword-var-do – Mad Angle Aug 06 '14 at 12:38
  • yes it's working for me with javascript, i thought it's possible also in php :) if not then i will try your answer now, sorry again for the trouble – Ayman Aug 06 '14 at 12:41
  • Its my pleasure. Please accept the answer if you are satisfied. – Mad Angle Aug 06 '14 at 12:42
  • @BlankHead, it didnt work :( can you help me with easier way ?! i have `config.php` file which will include the code you posted and `config.php` is included in `script.php` i need the `file.php` or `file.txt` which i think will be easier to make and add each `word` in new line to be read by `config.php` as `$value` to be read and process then by `script.php` as `$value` .and i'm sorry that i couldn't explain it more clear – Ayman Aug 06 '14 at 12:54
  • @Ayman You say above that your file is `var words = ["word1", "word2", "word3",];` that is **JS syntax**. If anything, that file should most likely read as `$words = array("word1", "word2", "word3",);` – Funk Forty Niner Aug 06 '14 at 13:32
1

Here you go. Let me know if you require an explanation about the code

<?php
$file = "filename.txt";
$content = file_get_contents($file);
$value = "";
foreach (explode("\n", $content) as $line) {
    // line is "word1", "word2", "word3", etc.
    $value .= $line . " ";
}
$value = trim($value); // Remove trailing space
Tom
  • 55
  • 5
0
foreach($lines as $value)

 $words= explode("\t",$value);`

use explode inside for each

Torrezzzz
  • 307
  • 2
  • 13