-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

2
  • 1
    file() reads a file into an array of lines. From there, you just need the first two array elements. Commented May 19, 2013 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 Commented May 19, 2013 at 13:25

1 Answer 1

2

Use this:-

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

foreach ($lines as $line_num => $line) {
    Do something....
}
Sign up to request clarification or add additional context in comments.

3 Comments

can you give any clearly example using ur answer of my problem?
still confused, how to adjust two text..
What confusion do you have ? To get text1 and text2 use $text= $lines[0] . $lines[1];

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.