-1

simply in php we could replace a string by other string such as using a particular function, str_replace (), etc

suppose I have a file.txt contains

text1
text2
text3
...
text n

and i have a variable $text such as:

$text = "I want to get text1 and text2";

How to get the strings (text1 and text2) based on the available strings in file.txt?

i want to have a new variable say ($new) which contains strings of $text that it fits on the string file.txt

i appreciate your help greatly.

thank you

Gumbo
  • 643,351
  • 109
  • 780
  • 844
user2126044
  • 81
  • 10
  • 1
    [`file()`](http://www.php.net/manual/en/function.file.php) reads a file into an array of lines. From there, you just need the first two array elements. – Michael Berkowski May 19 '13 at 13:17
  • Hi @MichaelBerkowski sorry before, i want to have a new variable say ($new) which contains strings of $text that it fits on the string file.txt – user2126044 May 19 '13 at 13:25

1 Answers1

2

Use this:-

$lines = file('textfile.txt');

foreach ($lines as $line_num => $line) {
    Do something....
}
Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49