Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-基于文件获取字符串_Php_String - Fatal编程技术网

PHP-基于文件获取字符串

PHP-基于文件获取字符串,php,string,Php,String,简单地说,在php中,我们可以用其他字符串替换字符串,例如使用特定函数str_replace()等 假设我有一个文件.txt包含 text1 text2 text3 ... text n 我有一个变量$text,例如: $text = "I want to get text1 and text2"; 如何根据file.txt中的可用字符串获取字符串(text1和text2) 我想要一个新的变量,比如($new),它包含字符串$text,它适合string file.txt 我非常感谢你的帮助

简单地说,在php中,我们可以用其他字符串替换字符串,例如使用特定函数str_replace()等

假设我有一个
文件.txt
包含

text1
text2
text3
...
text n
我有一个变量
$text
,例如:

$text = "I want to get text1 and text2";
如何根据file.txt中的可用字符串获取字符串(text1和text2)

我想要一个新的变量,比如($new),它包含字符串$text,它适合string file.txt

我非常感谢你的帮助

谢谢你使用这个:-

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

foreach ($lines as $line_num => $line) {
    Do something....
}

将文件读入行数组。从这里开始,您只需要前两个数组元素。您好@MichaelBerkowski对不起,我想有一个新的变量,比如($new),它包含字符串$text,它适合字符串文件。TXT您能用我的问题的答案给出任何清晰的例子吗?仍然困惑,如何调整两个文本..您有什么困惑?要获取text1和text2,请使用$text=$lines[0]$行[1];