PHP - 在文本文件中搜索字符串并将整行变成一个数组
PHP - Searching for string in text file and turn the whole line into an array
我想知道如何在文本文件中搜索字符串,然后将包含该字符串的整行转换为数组。为了不那么啰嗦,我只是举个例子。
sometext0:Test:sometext123
sometext0:Test14:sometext123
sometext0:test44:sometext123
本例中的字符串为 "Test14"(第二行)。应该做的是搜索 "Test14"。一旦完成,它应该将整行变成一个字符串,并以此为基础,将每行转换为每个分隔符的数组。
Array[0] 将是 "sometext0",Array[1] 将是 "Test14",而 Array[2] 将是 "sometext123"
谢谢!
要获取所需单词所在的整行,您可以在冒号上使用 strstr()
and pass in a -1
value for the third parameters. To split the line into an array, you can use explode()
。
这可以在下面看到:
<?php
$input = "sometext0:Test:sometext123
sometext0:Test14:sometext123
sometext0:test44:sometext123";
$target = "Test14";
$target_line = strstr($input, $target, -1);
var_dump(explode(":", $target_line));
哪个returns:
array(4) {
[0]=>
string(9) "sometext0"
[1]=>
string(4) "Test"
[2]=>
string(21) "sometext123
sometext0"
[3]=>
string(0) ""
}
这可以看出有效here.
我想知道如何在文本文件中搜索字符串,然后将包含该字符串的整行转换为数组。为了不那么啰嗦,我只是举个例子。
sometext0:Test:sometext123
sometext0:Test14:sometext123
sometext0:test44:sometext123
本例中的字符串为 "Test14"(第二行)。应该做的是搜索 "Test14"。一旦完成,它应该将整行变成一个字符串,并以此为基础,将每行转换为每个分隔符的数组。
Array[0] 将是 "sometext0",Array[1] 将是 "Test14",而 Array[2] 将是 "sometext123" 谢谢!
要获取所需单词所在的整行,您可以在冒号上使用 strstr()
and pass in a -1
value for the third parameters. To split the line into an array, you can use explode()
。
这可以在下面看到:
<?php
$input = "sometext0:Test:sometext123
sometext0:Test14:sometext123
sometext0:test44:sometext123";
$target = "Test14";
$target_line = strstr($input, $target, -1);
var_dump(explode(":", $target_line));
哪个returns:
array(4) {
[0]=>
string(9) "sometext0"
[1]=>
string(4) "Test"
[2]=>
string(21) "sometext123
sometext0"
[3]=>
string(0) ""
}
这可以看出有效here.